How can I check for GPS support in-App to add a feature for those with Location services enabled?

感情迁移 提交于 2019-12-25 00:37:37

问题


How can I check for GPS support in-App to add a feature for those with Location services enabled?

My concern is, I know I'd have to specify the tag in the manifest to declare that the app uses location services, but I still want the app to function for those without. I just want to check and, if the service is available, use it; otherwise just ignore that one feature.

Thanks.


回答1:


You can check if any Location services are enabled by looking to see if any location providers are enabled. I'm using this function in my code right now:

public static boolean areProvidersEnabled(Context context) {
    ContentResolver cr = context.getContentResolver();
    String providersAllowed = Settings.Secure.getString(cr, Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
    return providersAllowed != null && providersAllowed.length() > 0;
}

Another neat thing is that you can send the user straight to the Location settings and ask them if they want to enable it. I'll leave it to you on how to ask the user, but the Intent to get to the settings is like this:

Intent intent = new Intent(Settings.ACTION_SECURITY_SETTINGS);
startActivity(intent);


来源:https://stackoverflow.com/questions/2899621/how-can-i-check-for-gps-support-in-app-to-add-a-feature-for-those-with-location

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!