简体   繁体   English

如何在应用程序中检查GPS支持以为启用了定位服务的用户添加功能?

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

How can I check for GPS support in-App to add a feature for those with Location services enabled? 如何在应用程序中检查GPS支持以为启用了定位服务的用户添加功能?

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. 谢谢。

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

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

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