开发者

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

开发者 https://www.devze.com 2023-01-01 08:54 出处:网络
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 thetag in t开发者_运维问答he manifest to declare that the

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 t开发者_运维问答he 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.


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);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号