繁体   English   中英

我如何检查用户是否在Android手机上真正启用了GPS位置

[英]How can i check user really enabled the gps location on his phone in android

我的应用程序需要GPS,因此我已重定向用户以启用GPS,以防它关闭。

AlertDialog.Builder dialog = new AlertDialog.Builder(contxt);
            dialog.setMessage("Enable GPS Location Service");
            dialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface paramDialogInterface, int paramInt) {
                        // TODO Auto-generated method stub
                        Intent myIntent = new Intent( Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                        contxt.startActivity(myIntent);
                    }
                });

问题是用户从弹出窗口转到了位置设置,并且如果用户未从位置设置激活GPS,而是用户按下了返回按钮并返回到应用程序。再次返回到应用程序而不是激活GPS手段,该应用程序必须再次显示该对话框。

所以我再次确保如下所示,

GetLocationService();

        LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE );
        boolean statusOfGPS = manager.isProviderEnabled(LocationManager.GPS_PROVIDER);

        if(statusOfGPS==false)
            GetLocationService();

但是用户再次通过位置设置无法启用GPS,并且无法通过单击“后退”按钮并返回应用程序来启用GPS。 我如何强迫用户启用GPS并与应用程序一起使用。

我如何强迫用户启用GPS并与应用程序一起使用。

要在移动应用程序中的下一个屏幕之前强制用户启用GPS:

1.创建一种方法,在该方法中启用或禁用GPS检查,并相应地开始下一个活动:

private void isGPSEnabled(){
     LocationManager manager;
     boolean statusOfGPS =  
           manager.isProviderEnabled(LocationManager.GPS_PROVIDER);
     if(statusOfGPS){
        // start next Activity
      }else{
         // show alert for enabling GPS
      }
  }

2.AlertDialog肯定按钮上,使用startActivityForResult单击启动GPS设置屏幕:

@Override
 public void onClick(DialogInterface paramDialogInterface, int paramInt) {
    // TODO Auto-generated method stub
    Intent myIntent = new Intent( Settings.ACTION_LOCATION_SOURCE_SETTINGS);
    contxt.startActivityForResult(myIntent,100);
 }

3.当用户从“设置”返回到应用程序时,重写onActivityResult以检查GPS是否启用:

@Override
    protected void onActivityResult(int requestCode,int resultCode,Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        isGPSEnabled();
    }

同时使AlertDialog可取消false形式返回并触摸外部Dialog:

dialog.setCanceledOnTouchOutside(false);
dialog.setCancelable(false);

暂无
暂无

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

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