【问题标题】:Displaying Allow app to access device's location显示允许应用访问设备的位置
【发布时间】:2017-06-21 17:23:52
【问题描述】:

我是 android 编程新手,我需要显示警报,以便用户在点击允许按钮时自动授予我的应用程序权限。

喜欢这个应用

但实际上我的代码会发出一条消息,只会将用户带到设置,以便他/她可以手动更改它,并不是每个人都知道如何手动进行

我怎样才能获得第一个警报,以便应用程序自动获得权限,而不是我目前将用户移动到设置页面并且他们必须手动完成的方式

这是我的代码

  public void showSettingsAlert() {
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(_activity);
        alertDialog.setTitle("GPS Settings");
        alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu ?");

        alertDialog.setPositiveButton("Settings",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent = new Intent(
                                Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                        _activity.startActivity(intent);
                    }
                });

        alertDialog.setNegativeButton("Cancel",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });

        alertDialog.show();
    }

【问题讨论】:

标签: android permissions location android-gps


【解决方案1】:

允许您的应用访问设备的位置(第一个屏幕截图)与启用 GPS(第二个屏幕截图)不同。您实际上需要两者:首先检查您是否拥有其他答案所建议的权限,然后检查您是否已经启用了 GPS。

如果您想以编程方式启用 GPS,您需要使用“设置 API”。查看this answer 了解如何实现它。

【讨论】:

    【解决方案2】:

    您必须向用户请求许可,如下所示

    // Here, thisActivity is the current activity
    if (ContextCompat.checkSelfPermission(thisActivity,
                Manifest.permission.READ_CONTACTS)
        != PackageManager.PERMISSION_GRANTED) {
    
    // Should we show an explanation?
    if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
            Manifest.permission.READ_CONTACTS)) {
    
        // Show an explanation to the user *asynchronously* -- don't block
        // this thread waiting for the user's response! After the user
        // sees the explanation, try again to request the permission.
    
    } else {
    
        // No explanation needed, we can request the permission.
    
        ActivityCompat.requestPermissions(thisActivity,
                new String[]{Manifest.permission.READ_CONTACTS},
                MY_PERMISSIONS_REQUEST_READ_CONTACTS);
    
        // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
        // app-defined int constant. The callback method gets the
        // result of the request.
    }
    }
    

    这里是参考link

    【讨论】:

      【解决方案3】:

      您好,它是运行时许可,只需通过此链接获取参考click here

      也可以查看click here

      为了保护用户,他们必须在运行时获得授权,以便用户知道这是否与他的行为有关。

      为此:

      ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
      

      它将显示一个对话框,用户将在其中选择是否自动让您的应用使用位置信息。

      然后通过这个函数得到用户的答案:

          public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
          switch (requestCode) {
              case 1: {
                  // If request is cancelled, the result arrays are empty.
                  if (grantResults.length > 0
                  && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
      
                  } else {
                      // permission denied, boo! Disable the
                      // functionality that depends on this permission.
                  }
              return;
              }
                  // other 'case' lines to check for other
                  // permissions this app might request
          }
      }
      

      如果用户接受一次,那么您的应用程序会记住它,您将不再需要发送此 DialogBox。请注意,如果他决定,用户可以稍后禁用它。然后在请求位置之前,您必须测试是否仍然授予权限:

      public boolean checkLocationPermission()
      {
          String permission = "android.permission.ACCESS_FINE_LOCATION";
          int res = this.checkCallingOrSelfPermission(permission);
          return (res == PackageManager.PERMISSION_GRANTED);
      }
      

      【讨论】:

      • 一旦链接断开,你的答案就没有用了!
      猜你喜欢
      • 2018-01-06
      • 1970-01-01
      • 2017-11-12
      • 1970-01-01
      • 1970-01-01
      • 2016-10-15
      • 1970-01-01
      • 2012-04-16
      • 1970-01-01
      相关资源
      最近更新 更多