如何检查GPS是否被禁用Android [英] How to check if GPS is Disabled Android

查看:223
本文介绍了如何检查GPS是否被禁用Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个文件MainActivity中的MainActivity.java和HomeFragment.java调用HomeFragment中的一个函数,要求用户打开手机上的位置服务。问题在于,即使用户已经打开位置功能,仍然会调用该功能。有没有一种方法可以实现,只有当位置功能关闭时,才能启动HomeFragment的功能。



HomeFragment.java

  public static void displayPromptForEnablingGPS(
final Activity activity)
{
final AlertDialog.Builder builder =
AlertDialog.Builder(activity);
final String action = Settings.ACTION_LOCATION_SOURCE_SETTINGS;
final String message =启用GPS或任何其他位置
+服务以查找当前位置。单击确定以转到
+位置服务设置以允许您执行此操作。 ;


builder.setMessage(message)
.setPositiveButton(OK,
new DialogInterface.OnClickListener(){
public void onClick(DialogInterface d ,int id){
activity.startActivity(new Intent(action));
d.dismiss();
}
});

builder.create()。show();
}

MainActivity.java

  public void showMainView(){
HomeFragment.displayPromptForEnablingGPS(this);
}

谢谢:)

  final LocationManager manager =(LocationManager )getSystemService(Context.LOCATION_SERVICE); 

if(!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
//调用您的提示信息
}

这应该有用。



要检查定位服务是否已启用,您可以使用类似于这:

 字符串locationProviders = Settings.Secure.getString(getContentResolver(),Settings.Secure.LOCATION_PROVIDERS_ALLOWED); 
if(locationProviders == null || locationProviders.equals()){
...
startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));

$ / code>

来源:
Marcus'Post发现这里


I have two files MainActivity.java and HomeFragment.java in the MainActivity calls a function from HomeFragment that asks the user to turn on the location services on their phone. The problem is even when the user has the location feature already turned on the function is called anyway. Is there a way I can make it so only when the location feature is off that the function from the HomeFragment is launched.

HomeFragment.java

public static void displayPromptForEnablingGPS(
        final Activity activity)
{
    final AlertDialog.Builder builder =
            new AlertDialog.Builder(activity);
    final String action = Settings.ACTION_LOCATION_SOURCE_SETTINGS;
    final String message = "Enable either GPS or any other location"
            + " service to find current location.  Click OK to go to"
            + " location services settings to let you do so.";


    builder.setMessage(message)
            .setPositiveButton("OK",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface d, int id) {
                            activity.startActivity(new Intent(action));
                            d.dismiss();
                        }
                    });

    builder.create().show();
}

MainActivity.java

public void showMainView() {
    HomeFragment.displayPromptForEnablingGPS(this);
}

Thank you :)

解决方案

You could use something like this:

final LocationManager manager = (LocationManager) getSystemService( Context.LOCATION_SERVICE );

if ( !manager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) {
    // Call your Alert message
}

That should do the trick.

To check if Location Services are enabled, you can use code similar to this:

String locationProviders = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if (locationProviders == null || locationProviders.equals("")) {
...
startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}

Source: Marcus' Post found here

这篇关于如何检查GPS是否被禁用Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆