Android 6.0 申請許可權
阿新 • • 發佈:2019-01-05
如今6.0的手機越來越多,做app 就要適配6.0,6.0的需要自己去申請許可權。具體可以在載入引導頁面,就去申請。
首先,sdk必須是6.0以上的。
// 申請許可權的String[]
private String[] permissionArray = new String[] {Manifest.permission.CALL_PHONE,
Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_SMS};
申請許可權方法
/**
* Description:判斷是否開啟定位許可權
*
* @author kaiyinxu
* @time 5/24/2016 9:47 AM Comment:6.0後判斷許可權不同
*/
public void isPermissionLocal() {
// For Android < Android M, self permissions are always granted.
try {
final PackageInfo info = this.getPackageManager().getPackageInfo(this.getPackageName(), 0);
int targetSdkVersion = info.applicationInfo.targetSdkVersion;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (targetSdkVersion >= Build.VERSION_CODES.M) {
int permissionCheck = ContextCompat.checkSelfPermission(LoadingActivity.this,
Manifest.permission.ACCESS_COARSE_LOCATION);
// 許可權未允許
if (permissionCheck != 0) {
PermissionUtils.checkPermissionArray(LoadingActivity.this, permissionArray, 2);
} else {
// 申請許可權後的操作
getData();
}
} else {
// targetSdkVersion < Android M, we have to use PermissionChecker
// 申請許可權後的操作
getData();
}
} else {
// 申請許可權後的操作
getData();
}
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
}
許可權成功或失敗的回撥
/**
* Description:許可權回撥
*
* @author kaiyinxu
* @time 5/26/2016 10:36 AM Comment:
*/
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case PermissionUtils.PERMISSION_REQUEST_CODE:
if (PermissionUtils.verifyPermissions(grantResults)) {
// Permission Granted
// do you action
} else {
// Permission Denied
// Toast.makeText(this, "WRITE_CONTACTS Denied", Toast.LENGTH_SHORT).show();
}
// 申請許可權後的操作
getData();
break;
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
當然還有申請許可權的utils
package com.kaiyinxu.permisson.utils;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.util.Log;
import java.util.ArrayList;
/**
* Created by kaiyinxu on 16-25-5.
*/
public class PermissionUtils {
private static final String TAG = "PermissionUtils";
public static final int PERMISSION_REQUEST_CODE = 2;
public static final int PERMISSION_SETTING_REQ_CODE = 0x1000;
@TargetApi(Build.VERSION_CODES.M)
public static boolean checkPermission(Object cxt, String permission, int requestCode) {
if (!checkSelfPermissionWrapper(cxt, permission)) {
if (!shouldShowRequestPermissionRationaleWrapper(cxt, permission)) {
requestPermissionsWrapper(cxt, new String[]{permission}, requestCode);
} else {
Log.d(TAG, "should show rational");
}
return false;
}
return true;
}
private static void requestPermissionsWrapper(Object cxt, String[] permission, int requestCode) {
if (cxt instanceof Activity) {
Activity activity = (Activity) cxt;
ActivityCompat.requestPermissions(activity, permission, requestCode);
} else if (cxt instanceof Fragment) {
Fragment fragment = (Fragment) cxt;
fragment.requestPermissions(permission, requestCode);
} else {
throw new RuntimeException("cxt is net a activity or fragment");
}
}
private static boolean shouldShowRequestPermissionRationaleWrapper(Object cxt, String permission) {
if (cxt instanceof Activity) {
Activity activity = (Activity) cxt;
return ActivityCompat.shouldShowRequestPermissionRationale(activity,
permission);
} else if (cxt instanceof Fragment) {
Fragment fragment = (Fragment) cxt;
return fragment.shouldShowRequestPermissionRationale(permission);
} else {
throw new RuntimeException("cxt is net a activity or fragment");
}
}
@TargetApi(Build.VERSION_CODES.M)
private static boolean checkSelfPermissionWrapper(Object cxt, String permission) {
if (cxt instanceof Activity) {
Activity activity = (Activity) cxt;
return ActivityCompat.checkSelfPermission(activity,
permission) == PackageManager.PERMISSION_GRANTED;
} else if (cxt instanceof Fragment) {
Fragment fragment = (Fragment) cxt;
return fragment.getActivity().checkSelfPermission(permission) == PackageManager.PERMISSION_GRANTED;
} else {
throw new RuntimeException("cxt is net a activity or fragment");
}
}
private static String[] checkSelfPermissionArray(Object cxt, String[] permission) {
ArrayList<String> permiList = new ArrayList<String>();
for (String p : permission) {
if (!checkSelfPermissionWrapper(cxt, p)) {
permiList.add(p);
}
}
return permiList.toArray(new String[permiList.size()]);
}
@TargetApi(Build.VERSION_CODES.M)
public static boolean checkPermissionArray(Object cxt, String[] permission, int requestCode) {
String[] permissionNo = checkSelfPermissionArray(cxt, permission);
if (permissionNo.length > 0) {
requestPermissionsWrapper(cxt, permissionNo, requestCode);
return false;
} else return true;
}
public static boolean verifyPermissions(int[] grantResults) {
// At least one result must be checked.
if (grantResults.length < 1) {
return false;
}
// Verify that each required permission has been granted, otherwise return false.
for (int result : grantResults) {
if (result != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
return true;
}
/**
* 檢測系統彈出許可權
*
* @param cxt
* @param req
* @return
*/
@TargetApi(Build.VERSION_CODES.M)
public static boolean checkSettingAlertPermission(Object cxt, int req) {
if (cxt instanceof Activity) {
Activity activity = (Activity) cxt;
if (!Settings.canDrawOverlays(activity.getBaseContext())) {
Log.i(TAG, "Setting not permission");
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse("package:" + activity.getPackageName()));
activity.startActivityForResult(intent, req);
return false;
}
} else if (cxt instanceof Fragment) {
Fragment fragment = (Fragment) cxt;
if (!Settings.canDrawOverlays(fragment.getActivity())) {
Log.i(TAG, "Setting not permission");
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse("package:" + fragment.getActivity().getPackageName()));
fragment.startActivityForResult(intent, req);
return false;
}
} else {
throw new RuntimeException("cxt is net a activity or fragment");
}
return true;
}
}
Android Studio 只要這些就行;Eclipse環境上,需要先把sdk變成6.0,就可以用了。