Android 如何以编程方式访问设备设置?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11985251/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to access device settings programmatically?
提问by auy
Is there any way to access device settings programmatically?
有没有办法以编程方式访问设备设置?
For Example: Settings > Developer Options > USB debugging > True/False
例如:设置 > 开发者选项 > USB 调试 > 真/假
Thanks in advance.
提前致谢。
Edit: USB debugging is just an example. Can I access every settings which is exist in Settings in device.
编辑:USB 调试只是一个例子。我可以访问设备设置中存在的所有设置吗?
采纳答案by nandeesh
Settings.Secure.putInt(getActivity().getContentResolver(),Settings.Secure.ADB_ENABLED, 1);
Settings.Secure.putInt(getActivity().getContentResolver(),Settings.Secure.ADB_ENABLED, 1);
But you need root access to do this. Only system apps have permission to change secure settings
但是您需要 root 访问权限才能执行此操作。只有系统应用有权更改安全设置
Edit: Try this to Show all ANRs
编辑:试试这个以显示所有 ANR
Settings.Secure.putInt(ContentResolver,Settings.Secure.ANR_SHOW_BACKGROUND,0);
Settings.Secure.putInt(ContentResolver,Settings.Secure.ANR_SHOW_BACKGROUND,0);
and for Don't keep activities
和不要保持活动
ActivityManagerNative.getDefault().setAlwaysFinish(true)
ActivityManagerNative.getDefault().setAlwaysFinish(true)
but ActivityManagerNative is hidden in standard api set. you will need to do either reflection or use this methodto get android.jar. Again I guess this needs root access
但 ActivityManagerNative 隐藏在标准 api 集中。您将需要进行反射或使用此方法来获取 android.jar。我想这需要root访问权限
回答by ρяσ?ρ?я K
Try as:
尝试如下:
?int adb = Settings.Secure.getInt(context.getContentResolver(),
Settings.Secure.ADB_ENABLED, 0);
?// toggle the USB debugging setting
adb = adb == 0 ? 1 : 0;
?Settings.Secure.putInt(context.getContentResolver(),
Settings.Secure.ADB_ENABLED, adb);
In AndroidManifest.xml:
在 AndroidManifest.xml 中:
<uses-permission android:name="android.permission.WRITE_SETTINGS"></uses-permission>
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS"></uses-permission>
and finally look at this example
最后看看这个例子
回答by leet
I know that this question is old but is interesting.
我知道这个问题很老但很有趣。
Nandeesh's solution works well. But it does not work if you want to change certain settings such as USB Debugging. You have to use the following permission.
Nandeesh 的解决方案效果很好。但如果您想更改某些设置(例如 USB 调试),则它不起作用。您必须使用以下权限。
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS"/>
But, it doesn't work with non system apps. However, it is POSSIBLE to do this if you have root access. You can try thissolution where the author solves the exact same thing.
但是,它不适用于非系统应用程序。但是,如果您具有 root 访问权限,则可以执行此操作。你可以试试这个解决方案,作者解决了完全相同的问题。
You can also try https://github.com/alseambusher/adb-toggle
回答by Hanan
If you app is System app then you Simply do this to enable/disable USB Debugging
如果您的应用程序是系统应用程序,那么您只需执行此操作即可启用/禁用 USB 调试
Settings.Secure.putInt(getActivity().getContentResolver(),Settings.Secure.ADB_ENABLED, 1);
Settings.Secure.putInt(getActivity().getContentResolver(),Settings.Secure.ADB_ENABLED, 1);
You also need root access to do this. I have rooted device and I just simply execute SUto enable/disable Usb Debugging.
您还需要 root 访问权限才能执行此操作。我已经植根设备,我只是简单地执行SU来启用/禁用 Usb 调试。
Process proc = Runtime.getRuntime().exec(new String [] { "su", "-c", "setprop", "persist.service.adb.enable", "0"});
proc.waitFor();
Process proc2 = Runtime.getRuntime().exec(new String [] { "su", "-c", "stop", "adbd"});
proc2.waitFor();
Process proc = Runtime.getRuntime().exec(new String [] { "su", "-c", "setprop", "persist.service.adb.enable", "0"});
proc.waitFor();
Process proc2 = Runtime.getRuntime().exec(new String [] { "su", "-c", "stop", "adbd"});
proc2.waitFor();
Good thing is you don'tneed to place your application in (/system/priv-app/)Your simple app i.e. (/data/app)can do this.
好东西是你没有需要将您的应用程序(/系统/私法-应用程序/)你简单的应用程序,即(/数据/应用程序)可以做到这一点。
回答by VishalKale
You can't turn on the USB Debugging unless your app is system app.
除非您的应用程序是系统应用程序,否则您无法打开 USB 调试。
回答by Milad Faridnia
This line of code may help:
这行代码可能会有所帮助:
Settings.Global.putInt(getContentResolver(), Global.ADB_ENABLED, 1);
and you need this permission:
你需要这个权限:
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS"/>
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS"/>
but after adding this permission in manifest you will get this error: Permission is only granted to system apps
但是在清单中添加此权限后,您将收到此错误:权限仅授予系统应用程序
which means your app must be a system app.
这意味着您的应用程序必须是系统应用程序。