问题描述
我正在使用位置服务,我想知道如何检查用户单击按钮“允许或拒绝权限对话框”? 或开启或关闭该应用程序的定位服务?
1楼
是的,很容易做到。 请参阅 从文档复制的以下代码显示了如何检查用户是否批准或取消权限请求:
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_READ_CONTACTS: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the
// contacts-related task you need to do.
} 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
}
}