2017-02-04 95 views
2

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

喜欢这个程序

enter image description here

但实际上我的代码确实的消息,这将只是把用户的设置,这样他/她可以手动更改它,而不是每个人都知道该怎么做手工

enter image description here

我怎样才能得到第一个警报,这样该应用增益权限自动代替我现在的方式它只是将用户移动到设置页面,他们甲肝E要做到这一点手动

这里是我的代码

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(); 
    } 
+0

最简单的解决方案http://stackoverflow.com/a/40462527/1677824 – Akhil

回答

1

你必须从用户请求权限如下

// 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

1

喜它是一个运行时间许可,只是去通过这个环节做参考click here

还要检查这个click here

为了保护用户,他们必须在运行时被授权,所以用户知道它是否与他的行为有关。

要做到这一点:

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

它会显示一个对话框中,用户会选择羯羊他autorize您的应用程序使用位置或不。

然后通过使用该功能获得用户回答:

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 
    } 
} 

如果用户接受了一次,那么你的应用程序会记住它,你将不需要再发送此对话框。请注意,如果用户决定,用户可以稍后禁用它。然后请求该位置之前,你必须以测试权限仍不以为然:

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

一旦链接被打破,没有使用您的答案! –

1

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

如果要以编程方式启用GPS,则需要使用“设置API”。检查this answer如何实施它。

相关问题