Android M 权限,return 错误结果
Android M permissions, return incorrect result
我在我的 Andorid M 设备应用程序中拥有以下权限。我已经在 6.0.0 和 6.0.1 上对此进行了测试,并且在 Nexus 5X 和 Nexus 6P 上都得到了相同的结果。基本上每个权限 return 的错误结果和当我 enable/disable 它们时,结果不会改变。
我将在下面粘贴一些示例测试代码来展示我是如何测试它的。
清单权限:
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.NFC" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.BIND_NFC_SERVICE" />
<uses-permission android:name="com.samsung.android.providers.context.permission.WRITE_USE_APP_FEATURE_SURVEY" />
<uses-permission android:name="android.permission.RECEIVE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.simplytapp.virtualcard.permission.C2D_MESSAGE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
主要活动:
private static final String[] CHECK_PERMISSIONS = {
Manifest.permission.READ_PHONE_STATE,
Manifest.permission.CALL_PHONE,
Manifest.permission.READ_CALL_LOG,
Manifest.permission.WRITE_CALL_LOG,
Manifest.permission.USE_SIP,
Manifest.permission.PROCESS_OUTGOING_CALLS,
Manifest.permission.CALL_PRIVILEGED,
Manifest.permission.MODIFY_PHONE_STATE,
Manifest.permission.VIBRATE,
Manifest.permission.INTERNET,
Manifest.permission.NFC,
Manifest.permission.ACCESS_NETWORK_STATE,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.BIND_NFC_SERVICE,
Manifest.permission.WAKE_LOCK,
Manifest.permission.READ_CONTACTS,
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.ACCESS_FINE_LOCATION
};
for (int i = 0; i < CHECK_PERMISSIONS.length; i++) {
permissionsController.checkAccessPermissions(0, CHECK_PERMISSIONS[i]);
}
public boolean checkAccessPermissions(int statusCode, String permission) {
boolean granted = true;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ActivityCompat.checkSelfPermission(mContext, permission) != PackageManager.PERMISSION_GRANTED) {
granted = false;
}
}
Log.i("Permissions Check", "Permission: " + permission + " - has status: " + granted);
return granted;
}
这将始终return授予或不授予天气权限相同的结果。
Permissions Check: Permission: android.permission.READ_PHONE_STATE - has status: true
Permissions Check: Permission: android.permission.CALL_PHONE - has status: true
Permissions Check: Permission: android.permission.READ_CALL_LOG - has status: false
Permissions Check: Permission: android.permission.WRITE_CALL_LOG - has status: false
Permissions Check: Permission: android.permission.USE_SIP - has status: false
Permissions Check: Permission: android.permission.PROCESS_OUTGOING_CALLS - has status: false
Permissions Check: Permission: android.permission.CALL_PRIVILEGED - has status: false
Permissions Check: Permission: android.permission.MODIFY_PHONE_STATE - has status: false
Permissions Check: Permission: android.permission.VIBRATE - has status: true
Permissions Check: Permission: android.permission.INTERNET - has status: true
Permissions Check: Permission: android.permission.NFC - has status: true
Permissions Check: Permission: android.permission.ACCESS_NETWORK_STATE - has status: true
Permissions Check: Permission: android.permission.WRITE_EXTERNAL_STORAGE - has status: true
Permissions Check: Permission: android.permission.BIND_NFC_SERVICE - has status: false
Permissions Check: Permission: android.permission.WAKE_LOCK - has status: true
Permissions Check: Permission: android.permission.READ_CONTACTS - has status: true
Permissions Check: Permission: android.permission.READ_EXTERNAL_STORAGE - has status: true
Permissions Check: Permission: android.permission.ACCESS_FINE_LOCATION - has status: true
您需要为目标 api 22 编译您的应用,或者您应该在 运行 时请求权限。参见:http://developer.android.com/training/permissions/requesting.html
这只是google地图许可位置的示例之一。你可以在上面添加更多以检查其他。
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && getActivity().checkSelfPermission( Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED && getActivity().checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
if(!shouldShowRequestPermissionRationale(Manifest.permission.ACCESS_FINE_LOCATION)) {
alertOpenSetting();
}else {
checkLocationPermission();
ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_CODE_ASK_PERMISSIONS);
}
}
public boolean checkLocationPermission()
{
String permission = "android.permission.ACCESS_FINE_LOCATION";
int res = getActivity().checkCallingOrSelfPermission(permission);
return (res == PackageManager.PERMISSION_GRANTED);
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case 0: {
//null or 0
}
case 1: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
insertPermissionWrapper();
NetworkManager.getInstance().startLocationService();
}else {
//denied
}
return;
}
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
private void alertOpenSetting() {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity());
alertDialogBuilder.setTitle("Location, Storage and Telephone permissions are required to use this app.");
alertDialogBuilder.setMessage("Please enable these permissions in Permissions under app settings.");
alertDialogBuilder.setNegativeButton("Go to setting", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
goToSettings();
}
});
alertDialogBuilder.create();
alertDialogBuilder.show();
}
希望这对您有所帮助
我在我的 Andorid M 设备应用程序中拥有以下权限。我已经在 6.0.0 和 6.0.1 上对此进行了测试,并且在 Nexus 5X 和 Nexus 6P 上都得到了相同的结果。基本上每个权限 return 的错误结果和当我 enable/disable 它们时,结果不会改变。
我将在下面粘贴一些示例测试代码来展示我是如何测试它的。
清单权限:
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.NFC" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.BIND_NFC_SERVICE" />
<uses-permission android:name="com.samsung.android.providers.context.permission.WRITE_USE_APP_FEATURE_SURVEY" />
<uses-permission android:name="android.permission.RECEIVE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.simplytapp.virtualcard.permission.C2D_MESSAGE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
主要活动:
private static final String[] CHECK_PERMISSIONS = {
Manifest.permission.READ_PHONE_STATE,
Manifest.permission.CALL_PHONE,
Manifest.permission.READ_CALL_LOG,
Manifest.permission.WRITE_CALL_LOG,
Manifest.permission.USE_SIP,
Manifest.permission.PROCESS_OUTGOING_CALLS,
Manifest.permission.CALL_PRIVILEGED,
Manifest.permission.MODIFY_PHONE_STATE,
Manifest.permission.VIBRATE,
Manifest.permission.INTERNET,
Manifest.permission.NFC,
Manifest.permission.ACCESS_NETWORK_STATE,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.BIND_NFC_SERVICE,
Manifest.permission.WAKE_LOCK,
Manifest.permission.READ_CONTACTS,
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.ACCESS_FINE_LOCATION
};
for (int i = 0; i < CHECK_PERMISSIONS.length; i++) {
permissionsController.checkAccessPermissions(0, CHECK_PERMISSIONS[i]);
}
public boolean checkAccessPermissions(int statusCode, String permission) {
boolean granted = true;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ActivityCompat.checkSelfPermission(mContext, permission) != PackageManager.PERMISSION_GRANTED) {
granted = false;
}
}
Log.i("Permissions Check", "Permission: " + permission + " - has status: " + granted);
return granted;
}
这将始终return授予或不授予天气权限相同的结果。
Permissions Check: Permission: android.permission.READ_PHONE_STATE - has status: true
Permissions Check: Permission: android.permission.CALL_PHONE - has status: true
Permissions Check: Permission: android.permission.READ_CALL_LOG - has status: false
Permissions Check: Permission: android.permission.WRITE_CALL_LOG - has status: false
Permissions Check: Permission: android.permission.USE_SIP - has status: false
Permissions Check: Permission: android.permission.PROCESS_OUTGOING_CALLS - has status: false
Permissions Check: Permission: android.permission.CALL_PRIVILEGED - has status: false
Permissions Check: Permission: android.permission.MODIFY_PHONE_STATE - has status: false
Permissions Check: Permission: android.permission.VIBRATE - has status: true
Permissions Check: Permission: android.permission.INTERNET - has status: true
Permissions Check: Permission: android.permission.NFC - has status: true
Permissions Check: Permission: android.permission.ACCESS_NETWORK_STATE - has status: true
Permissions Check: Permission: android.permission.WRITE_EXTERNAL_STORAGE - has status: true
Permissions Check: Permission: android.permission.BIND_NFC_SERVICE - has status: false
Permissions Check: Permission: android.permission.WAKE_LOCK - has status: true
Permissions Check: Permission: android.permission.READ_CONTACTS - has status: true
Permissions Check: Permission: android.permission.READ_EXTERNAL_STORAGE - has status: true
Permissions Check: Permission: android.permission.ACCESS_FINE_LOCATION - has status: true
您需要为目标 api 22 编译您的应用,或者您应该在 运行 时请求权限。参见:http://developer.android.com/training/permissions/requesting.html
这只是google地图许可位置的示例之一。你可以在上面添加更多以检查其他。
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && getActivity().checkSelfPermission( Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED && getActivity().checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
if(!shouldShowRequestPermissionRationale(Manifest.permission.ACCESS_FINE_LOCATION)) {
alertOpenSetting();
}else {
checkLocationPermission();
ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_CODE_ASK_PERMISSIONS);
}
}
public boolean checkLocationPermission()
{
String permission = "android.permission.ACCESS_FINE_LOCATION";
int res = getActivity().checkCallingOrSelfPermission(permission);
return (res == PackageManager.PERMISSION_GRANTED);
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case 0: {
//null or 0
}
case 1: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
insertPermissionWrapper();
NetworkManager.getInstance().startLocationService();
}else {
//denied
}
return;
}
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
private void alertOpenSetting() {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity());
alertDialogBuilder.setTitle("Location, Storage and Telephone permissions are required to use this app.");
alertDialogBuilder.setMessage("Please enable these permissions in Permissions under app settings.");
alertDialogBuilder.setNegativeButton("Go to setting", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
goToSettings();
}
});
alertDialogBuilder.create();
alertDialogBuilder.show();
}
希望这对您有所帮助