【问题标题】:How can i check user really enabled the gps location on his phone in android我如何检查用户是否真的在他的手机上启用了 android 中的 gps 位置
【发布时间】:2016-03-05 13:11:12
【问题描述】:

我的应用程序需要 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 并使用应用程序。

【问题讨论】:

    标签: android gps


    【解决方案1】:

    如何强制用户启用 GPS 并使用 应用。

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

    1. 创建一个方法来检查 GPS 是否启用,并相应地启动下一个 Activity:

    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 Cancelable false 形成背面和外部对话框的触摸:

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

    【讨论】:

      猜你喜欢
      • 2014-04-26
      • 1970-01-01
      • 2014-09-10
      • 1970-01-01
      • 2013-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-26
      相关资源
      最近更新 更多