PermissionManage

  介绍

  如果设备运行的是 Android 6.0(API 级别 23)或更高版本,并且应用的 targetSdkVersion 是 23 或更高版本,则应用在运行时向用户请求权限。

  如果设备运行的是 Android 5.1(API 级别 22)或更低版本,并且应用的 targetSdkVersion 是 22 或更低版本,则系统会在用户安装应用时要求用户授予权限。

  参考资料

  https://developer.android.google.cn/guide/topics/security/permissions.html

  https://github.com/lovedise/PermissionGen

  https://github.com/tbruyelle/RxPermissions

  以下是需要单独申请的权限,共分为9组,每组只要有一个权限申请成功了,就默认整组权限都可以使用了

XML/HTML代码
  1.   group:android.permission-group.CONTACTS  
  2.     permission:android.permission.WRITE_CONTACTS  
  3.     permission:android.permission.GET_ACCOUNTS      
  4.     permission:android.permission.READ_CONTACTS  
  5.   
  6.   group:android.permission-group.PHONE  
  7.     permission:android.permission.READ_CALL_LOG  
  8.     permission:android.permission.READ_PHONE_STATE   
  9.     permission:android.permission.CALL_PHONE  
  10.     permission:android.permission.WRITE_CALL_LOG  
  11.     permission:android.permission.USE_SIP  
  12.     permission:android.permission.PROCESS_OUTGOING_CALLS  
  13.     permission:com.android.voicemail.permission.ADD_VOICEMAIL  
  14.   
  15.   group:android.permission-group.CALENDAR  
  16.     permission:android.permission.READ_CALENDAR  
  17.     permission:android.permission.WRITE_CALENDAR  
  18.   
  19.   group:android.permission-group.CAMERA  
  20.     permission:android.permission.CAMERA  
  21.   
  22.   group:android.permission-group.SENSORS  
  23.     permission:android.permission.BODY_SENSORS  
  24.   
  25.   group:android.permission-group.LOCATION  
  26.     permission:android.permission.ACCESS_FINE_LOCATION  
  27.     permission:android.permission.ACCESS_COARSE_LOCATION  
  28.   
  29.   group:android.permission-group.STORAGE  
  30.     permission:android.permission.READ_EXTERNAL_STORAGE  
  31.     permission:android.permission.WRITE_EXTERNAL_STORAGE  
  32.   
  33.   group:android.permission-group.MICROPHONE  
  34.     permission:android.permission.RECORD_AUDIO  
  35.   
  36.   group:android.permission-group.SMS  
  37.     permission:android.permission.READ_SMS  
  38.     permission:android.permission.RECEIVE_WAP_PUSH  
  39.     permission:android.permission.RECEIVE_MMS  
  40.     permission:android.permission.RECEIVE_SMS  
  41.     permission:android.permission.SEND_SMS  
  42.     permission:android.permission.READ_CELL_BROADCASTS  

  以下是普通权限,只需要在AndroidManifest.xml中申请即可

XML/HTML代码
  1.  android.permission.ACCESS_LOCATION_EXTRA_COMMANDS  
  2.  android.permission.ACCESS_NETWORK_STATE  
  3.  android.permission.ACCESS_NOTIFICATION_POLICY  
  4.  android.permission.ACCESS_WIFI_STATE  
  5.  android.permission.ACCESS_WIMAX_STATE  
  6.  android.permission.BLUETOOTH  
  7.  android.permission.BLUETOOTH_ADMIN  
  8.  android.permission.BROADCAST_STICKY  
  9.  android.permission.CHANGE_NETWORK_STATE  
  10.  android.permission.CHANGE_WIFI_MULTICAST_STATE  
  11.  android.permission.CHANGE_WIFI_STATE  
  12.  android.permission.CHANGE_WIMAX_STATE  
  13.  android.permission.DISABLE_KEYGUARD  
  14.  android.permission.EXPAND_STATUS_BAR  
  15.  android.permission.FLASHLIGHT  
  16.  android.permission.GET_ACCOUNTS  
  17.  android.permission.GET_PACKAGE_SIZE  
  18.  android.permission.INTERNET  
  19.  android.permission.KILL_BACKGROUND_PROCESSES  
  20.  android.permission.MODIFY_AUDIO_SETTINGS  
  21.  android.permission.NFC  
  22.  android.permission.READ_SYNC_SETTINGS  
  23.  android.permission.READ_SYNC_STATS  
  24.  android.permission.RECEIVE_BOOT_COMPLETED  
  25.  android.permission.REORDER_TASKS  
  26.  android.permission.REQUEST_INSTALL_PACKAGES  
  27.  android.permission.SET_TIME_ZONE  
  28.  android.permission.SET_WALLPAPER  
  29.  android.permission.SET_WALLPAPER_HINTS  
  30.  android.permission.SUBSCRIBED_FEEDS_READ  
  31.  android.permission.TRANSMIT_IR  
  32.  android.permission.USE_FINGERPRINT  
  33.  android.permission.VIBRATE  
  34.  android.permission.WAKE_LOCK  
  35.  android.permission.WRITE_SYNC_SETTINGS  
  36.  com.android.alarm.permission.SET_ALARM  
  37.  com.android.launcher.permission.INSTALL_SHORTCUT  
  38.  com.android.launcher.permission.UNINSTALL_SHORTCUT  

  示例代码

Java代码
  1. /** 
  2.  * BaseActivity 
  3.  *  
  4.  * @author Edwin.Wu 
  5.  * @version 2017/3/17$ 14:12$ 
  6.  * @since JDK1.8 
  7.  */  
  8. public class BaseActivity extends AppCompatActivity {  
  9.   
  10.     @Override  
  11.     protected void onCreate(@Nullable Bundle savedInstanceState) {  
  12.         super.onCreate(savedInstanceState);  
  13.     }  
  14.   
  15.     /** 
  16.      * 为子类提供一个权限检查方法 
  17.      * 
  18.      * @param permissions 
  19.      * @return 
  20.      */  
  21.     public boolean hasPermission(String... permissions) {  
  22.         for (String permission : permissions) {  
  23.             if (ActivityCompat.checkSelfPermission(this, permission)  
  24.                     != PackageManager.PERMISSION_GRANTED) {  
  25.                 return false;  
  26.             }  
  27.         }  
  28.         return true;  
  29.     }  
  30.   
  31.     /** 
  32.      * 权限请求申请 
  33.      * 
  34.      * @param requestCode 请求码 
  35.      * @param permissions 权限 
  36.      */  
  37.     public void requestPermission(int requestCode, String... permissions) {  
  38.         ActivityCompat.requestPermissions(this, permissions, requestCode);  
  39.     }  
  40.   
  41.     @Override  
  42.     public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {  
  43.         super.onRequestPermissionsResult(requestCode, permissions, grantResults);  
  44.         switch (requestCode) {  
  45.             case PermissionsConstans.WRITE_STORAGE_CODE:  
  46.                 if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {  
  47.                     doSDCardPermission();  
  48.                 } else {  
  49.                     //TODO 提示用户权限未授予  
  50.                     Toast.makeText(BaseActivity.this"WRITE_EXTERNAL_STORAGE 权限未开启", Toast.LENGTH_SHORT).show();  
  51.                 }  
  52.                 break;  
  53.             case PermissionsConstans.CALL_PHONE_CODE:  
  54.                 if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {  
  55.                     doCallPhone();  
  56.                 } else {  
  57.                     //TODO 提示用户权限未授予  
  58.                     Toast.makeText(BaseActivity.this"ACTION_CALL 权限未开启", Toast.LENGTH_SHORT).show();  
  59.                 }  
  60.                 break;  
  61.             default:  
  62.                 break;  
  63.         }  
  64.     }  
  65.   
  66.   
  67.     /** 
  68.      * 默认的写SD权限处理 
  69.      */  
  70.     protected void doSDCardPermission() {  
  71.         //TODO  
  72.     }  
  73.   
  74.     /** 
  75.      * 默认的打电话处理 
  76.      */  
  77.     protected void doCallPhone() {  
  78.         //TODO   
  79.     }  
  80. }
Java代码
  1. /** 
  2.  * BaseActivity 
  3.  * 
  4.  * @author Edwin.Wu 
  5.  * @version 2017/3/17 11:59 
  6.  * @since JDK1.8 
  7.  */  
  8. public class PermissionActivity extends BaseActivity {  
  9.   
  10.     @Override  
  11.     protected void onCreate(Bundle savedInstanceState) {  
  12.         super.onCreate(savedInstanceState);  
  13.         setContentView(R.layout.activity_permission);  
  14.     }  
  15.   
  16.     public void callPhone(View view) {  
  17.         if (hasPermission(android.Manifest.permission.CALL_PHONE)) {  
  18.             doCallPhone();  
  19.         } else {  
  20.             requestPermission(PermissionsConstans.CALL_PHONE_CODE, android.Manifest.permission.CALL_PHONE);  
  21.         }  
  22.     }  
  23.   
  24.     public void sdCardPermission(View view) {  
  25.         if (hasPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)) {  
  26.             doSDCardPermission();  
  27.         } else {  
  28.             requestPermission(PermissionsConstans.WRITE_STORAGE_CODE, android.Manifest.permission.WRITE_EXTERNAL_STORAGE);  
  29.         }  
  30.     }  
  31.   
  32.   
  33.     @Override  
  34.     protected void doCallPhone() {  
  35.         Intent intent = new Intent(Intent.ACTION_CALL);  
  36.         intent.setData(Uri.parse("tel:" + "10000"));  
  37.         startActivity(intent);  
  38.     }  
  39.   
  40.     @Override  
  41.     protected void doSDCardPermission() {  
  42.         OkHttpClient okHttpClient = new OkHttpClient();  
  43.         Request request = new Request  
  44.                 .Builder()  
  45.                 .url("http://img.mmjpg.com/2015/350/3.jpg")  
  46.                 .get()  
  47.                 .build();  
  48.         Call call = okHttpClient.newCall(request);  
  49.         call.enqueue(new Callback() {  
  50.             @Override  
  51.             public void onFailure(Call call, IOException e) {  
  52.   
  53.             }  
  54.   
  55.             @Override  
  56.             public void onResponse(Call call, Response response) throws IOException {  
  57.                 ResponseBody body = response.body();  
  58.                 InputStream is = body.byteStream();  
  59.                 int len = 0;  
  60.                 byte[] bytes = new byte[2048];  
  61.                 String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + System.currentTimeMillis() + ".jpg";  
  62.                 File file = new File(path);  
  63.                 FileOutputStream fos = new FileOutputStream(file);  
  64.                 while ((len = is.read(bytes)) != -1) {  
  65.                     fos.write(bytes, 0, len);  
  66.                     fos.flush();  
  67.                 }  
  68.   
  69.                 Toast.makeText(PermissionActivity.this"SDCard写入成功", Toast.LENGTH_SHORT).show();  
  70.                 Log.e("Edwin""SDCard写入成功");  
  71.             }  
  72.         });  
  73.   
  74.   
  75.     }  
  76.   
  77. }  
Java代码
  1. /** 
  2.  * PermissionGen框架 
  3.  * 
  4.  * @author Edwin.Wu 
  5.  * @version 2017/3/17 23:19 
  6.  * @since JDK1.8 
  7.  */  
  8. public class PermissionGenActivity extends AppCompatActivity {  
  9.   
  10.     @Override  
  11.     protected void onCreate(Bundle savedInstanceState) {  
  12.         super.onCreate(savedInstanceState);  
  13.         setContentView(R.layout.activity_permission_gen);  
  14.     }  
  15.   
  16.     public void openCamera(View view) {  
  17.         PermissionGen.needPermission(this200, Manifest.permission.CAMERA);  
  18.     }  
  19.   
  20.     public void openGroup(View view) {  
  21.         PermissionGen  
  22.                 .with(this)  
  23.                 .addRequestCode(100)  
  24.                 .permissions(  
  25.                         Manifest.permission.READ_CONTACTS,  
  26.                         Manifest.permission.RECEIVE_SMS,  
  27.                         Manifest.permission.CAMERA,  
  28.                         Manifest.permission.WRITE_CONTACTS)  
  29.                 .request();  
  30.     }  
  31.   
  32.   
  33.     @PermissionSuccess(requestCode = 100)  
  34.     public void openGroupSuccess() {  
  35.         Toast.makeText(this"Group Permission Success", Toast.LENGTH_SHORT).show();  
  36.         startActivity(new Intent(this, ContactActivity.class));  
  37.     }  
  38.   
  39.     @PermissionFail(requestCode = 100)  
  40.     private void openGroupFail() {  
  41.         Toast.makeText(this"Group permission is not granted", Toast.LENGTH_SHORT).show();  
  42.     }  
  43.   
  44.     @PermissionSuccess(requestCode = 200)  
  45.     public void openCameraSuccess() {  
  46.         Toast.makeText(this"Camera permission Success", Toast.LENGTH_SHORT).show();  
  47.     }  
  48.   
  49.     @PermissionFail(requestCode = 200)  
  50.     public void openCameraFail() {  
  51.         Toast.makeText(this"Camera permission is not granted", Toast.LENGTH_SHORT).show();  
  52.     }  
  53.   
  54.     @Override  
  55.     public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {  
  56.         PermissionGen.onRequestPermissionsResult(this, requestCode, permissions, grantResults);  
  57.     }  
  58. }
Java代码
  1. /** 
  2.  * RxPermission框架 
  3.  * 
  4.  * @author Edwin.Wu 
  5.  * @version 2017/3/19 14:17 
  6.  * @since JDK1.8 
  7.  */  
  8. public class RxPermissionActivity extends AppCompatActivity {  
  9.     private static final String TAG = "RxPermissionsSample";  
  10.     private Camera camera;  
  11.     private SurfaceView surfaceView;  
  12.   
  13.     @Override  
  14.     protected void onCreate(Bundle savedInstanceState) {  
  15.         super.onCreate(savedInstanceState);  
  16.   
  17.         RxPermissions rxPermissions = new RxPermissions(this);  
  18.         rxPermissions.setLogging(true);  
  19.   
  20.         setContentView(R.layout.activity_rx_permission);  
  21.   
  22.         surfaceView = (SurfaceView) findViewById(R.id.surfaceView);  
  23.   
  24.         RxView.clicks(findViewById(R.id.enableCamera))  
  25.                 // Ask for permissions when button is clicked  
  26.                 .compose(rxPermissions.ensureEach(Manifest.permission.CAMERA))  
  27.                 .subscribe(new Action1<Permission>() {  
  28.                                @Override  
  29.                                public void call(Permission permission) {  
  30.                                    Log.i(TAG, "Permission result " + permission);  
  31.                                    if (permission.granted) {  
  32.                                        releaseCamera();  
  33.                                        camera = Camera.open(0);  
  34.                                        try {  
  35.                                            camera.setPreviewDisplay(surfaceView.getHolder());  
  36.                                            camera.startPreview();  
  37.                                        } catch (IOException e) {  
  38.                                            Log.e(TAG, "Error while trying to display the camera preview", e);  
  39.                                        }  
  40.                                    } else if (permission.shouldShowRequestPermissionRationale) {  
  41.                                        // Denied permission without ask never again  
  42.                                        Toast.makeText(RxPermissionActivity.this"Denied permission without ask never again", Toast.LENGTH_SHORT).show();  
  43.                                    } else {  
  44.                                        // Denied permission with ask never again  
  45.                                        // Need to go to the settings  
  46.                                        Toast.makeText(RxPermissionActivity.this"Permission denied, can't enable the camera", Toast.LENGTH_SHORT).show();  
  47.                                    }  
  48.                                }  
  49.                            },  
  50.                         new Action1<Throwable>() {  
  51.                             @Override  
  52.                             public void call(Throwable t) {  
  53.                                 Log.e(TAG, "onError", t);  
  54.                             }  
  55.                         },  
  56.                         new Action0() {  
  57.                             @Override  
  58.                             public void call() {  
  59.                                 Log.i(TAG, "OnComplete");  
  60.                             }  
  61.                         });  
  62.     }  
  63.   
  64.     @Override  
  65.     protected void onStop() {  
  66.         super.onStop();  
  67.         releaseCamera();  
  68.     }  
  69.   
  70.     private void releaseCamera() {  
  71.         if (camera != null) {  
  72.             camera.release();  
  73.             camera = null;  
  74.         }  
  75.     }  
  76. }
本文发布:Android开发网
本文地址:http://www.jizhuomi.com/android/course/664.html
2017年3月22日
发布:鸡啄米 分类:Android开发教程 浏览: 评论:0