简体   繁体   English

Android设置。ACTION_LOCATION_SOURCE_SETTINGS导致崩溃

[英]Android Settings.ACTION_LOCATION_SOURCE_SETTINGS resulting in crash

I am using the following line of code to redirect user to location settings screen 我正在使用以下代码行将用户重定向到位置设置屏幕

startActivityForResult(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS), 0); 

It works fine on most devices, but crashes on some Samsung devices with an exception : 它在大多数设备上都能正常工作,但在某些三星设备上会崩溃,但有以下例外:

android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.settings.LOCATION_SOURCE_SETTINGS } android.content.ActivityNotFoundException:未找到处理意图的活动{act = android.settings.LOCATION_SOURCE_SETTINGS}

Is there a better way to handle location settings ? 有没有更好的方法来处理位置设置?

Is there a better way to handle location settings ? 有没有更好的方法来处理位置设置?

Pretty much all those action strings on the Settings class mention in the JavaDocs that the activity may not exist and so you should handle that appropriately (eg, wrap your startActivity() call in a ActivityNotFoundException handler). 在JavaDocs中, Settings类上的所有那些动作字符串几乎都提到该活动可能不存在,因此您应该适当地处理该活动(例如,将ActivityNotFoundException startActivity()调用包装在ActivityNotFoundException处理程序中)。

In this case, there isn't an obvious fallback option other than ACTION_SETTINGS . 在这种情况下,除了ACTION_SETTINGS之外,没有其他明显的后备选项。 You might display a Snackbar or dialog from the exception handler indicating that this device does not have direct third-party access to the location settings screen. 您可能会在异常处理程序中显示一个Snackbar或对话框,表明该设备没有第三方直接访问位置设置屏幕。

Also note that startActivityForResult() is useless in your situation; 还要注意, startActivityForResult()在您的情况下是无用的; just use startActivity() . 只需使用startActivity()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM