英文:
Can't detect if developer options are enabled in API 25?
问题
在Android Studio模拟器中(仅适用于API 25),我无法检测是否已启用开发者选项。
这段代码始终返回true,即使未启用开发者选项...我该如何修复这个问题?
int developerOptions = Settings.Secure.getInt(this.getContentResolver(), Settings.Global.DEVELOPMENT_SETTINGS_ENABLED , 0);
我尝试过Settings.Global.getInt(this.getContentResolver(), Settings.Global.DEVELOPMENT_SETTINGS_ENABLED , 0);
但仍然遇到相同的问题。
英文:
In the Android Studio emulator (API 25 only), I can't detect if developer options are enabled.
This code always returns true, even if developer options are not enabled... How can I fix this?
int developerOptions = Settings.Secure.getInt(this.getContentResolver(), Settings.Global.DEVELOPMENT_SETTINGS_ENABLED , 0);
I tried Settings.Global.getInt(this.getContentResolver(), Settings.Global.DEVELOPMENT_SETTINGS_ENABLED , 0);
but I still get the same issue.
答案1
得分: 1
以下是翻译好的部分:
应该在设备小于16的情况下换行:
int result = Settings.Secure.getInt(this.getContentResolver(),
Settings.Secure.DEVELOPMENT_SETTINGS_ENABLED, 0);
对于其他旧设备大于16(这是你的情况,也是失败的原因):
int result = Settings.Secure.getInt(this.getContentResolver(),
Settings.Global.DEVELOPMENT_SETTINGS_ENABLED , 0);
作为替代方案。你应该尝试在真实设备上,因为模拟器的不同goldfish状态可能会影响你的响应。
更新。
还有第二点,调用Activity
来启用开发者选项。如果失败了,应该将其禁用。当它存在时,你不需要打开它,可以立即关闭。例如:
try {
startActivity(new Intent(android.provider.Settings.ACTION_APPLICATION_DEVELOPMENT_SETTINGS), 8080);
// 你也可以设置一个延迟以完成活动。
finishActivity(8080);
// 活动存在的行为
} catch (Exception e) {
// 禁用开发者选项
}
英文:
It's should be done with next line for Devices <16:
int result = Settings.Secure.getInt(this.getContentResolver(),
Settings.Secure.DEVELOPMENT_SETTINGS_ENABLED, 0);
For other old devices >16 (it's your case, and that's why it fails)
int result = Settings.Secure.getInt(this.getContentResolver(),
Settings.Global.DEVELOPMENT_SETTINGS_ENABLED , 0);
As an alternative. You should 1. try on real devices, because different goldfish status of emulator might effect you response.
UPDATE.
And 2. Call Activity
for developer option. If it's failed it's should be disabled. When it is exist, you don't need to open it, and you can close immediately. For ex.
try {
startActivity(new Intent(android.provider.Settings.ACTION_APPLICATION_DEVELOPMENT_SETTINGS), 8080);
// You might also post a delay for finishing activity.
finishActivity(8080);
// Activity exist behavior
} catch (Exception e) {
// Disabled Developer options
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论