Android 敏感權限申請
阿新 • • 發佈:2019-03-08
lis com inter 用戶 whether any message forward import
動態權限管理是Android6.0(Build.VERSION_CODES.M = Api23)推出的,提醒用戶當前APP所需要的權限,防止濫用。這些權限一般分為三種:(1)普通權限:直接manifest清單文件中寫上註冊就行了 (2)危險權限:需要動態申請 (3)特殊權限:此處暫不說明。
註意:這裏我們就按照原生,和git上比較出名的第三方權限管理工具,EasyPermissions和RxPermissions
註意:這裏我們就按照原生,和git上比較出名的第三方權限管理工具,EasyPermissions和RxPermissions
以下是EasyPermissions的代碼樣例:基本形成閉環,就是禁止後二次打開彈框太醜,谷歌代碼的鍋
package com.example.testperms;import android.Manifest; import android.content.DialogInterface; import android.content.Intent; import android.net.Uri; import android.os.Build; import android.provider.Settings; import android.support.annotation.NonNull; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity;import android.os.Bundle; import android.util.Log; import java.util.List; import pub.devrel.easypermissions.AfterPermissionGranted; import pub.devrel.easypermissions.EasyPermissions; import pub.devrel.easypermissions.PermissionRequest; public class MainActivity extends AppCompatActivity implementsEasyPermissions.PermissionCallbacks { private String TAG = "MainActivity"; private static final int REQUEST_CODE_RUNTIME_PERMS = 0; boolean isDenied = false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); methodRequiresTwoPermission(); } @AfterPermissionGranted(REQUEST_CODE_RUNTIME_PERMS) private void methodRequiresTwoPermission() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { String[] perms = {Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}; if (EasyPermissions.hasPermissions(this, perms)) { // Already have permission, do the thing // ... } else { // Do not have permissions, request them now EasyPermissions.requestPermissions(this, "文件讀寫權限", REQUEST_CODE_RUNTIME_PERMS, perms); // EasyPermissions.requestPermissions( // new PermissionRequest.Builder(this, REQUEST_CODE_RUNTIME_PERMS, perms) // .setRationale(R.string.str_rw_perms) // .setPositiveButtonText(R.string.rationale_ask_ok) // .setNegativeButtonText(R.string.rationale_ask_cancel) //// .setTheme(R.style.Theme_AppCompat) // .build()); } } } @Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); // Forward results to EasyPermissions EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this); if (requestCode == REQUEST_CODE_RUNTIME_PERMS) { // 成功獲取運行時授權 } else { // 獲取運行時權限失敗 } } @Override public void onPermissionsGranted(int requestCode, List<String> perms) { Log.d(TAG, "onPermissionsGranted:" + requestCode + ":" + perms.size()); } @Override public void onPermissionsDenied(int requestCode, List<String> perms) { // methodRequiresTwoPermission(); Log.d(TAG, "onPermissionsDenied:" + requestCode + ":" + perms.size()); // (Optional) Check whether the user denied any permissions and checked "NEVER ASK AGAIN." // This will display a dialog directing them to enable the permission in app settings. if (EasyPermissions.somePermissionPermanentlyDenied(this, perms)) { new AlertDialog.Builder(this) .setTitle("權限提示") .setMessage("本項認證需要外置存儲權限,點擊確認後在新頁面滑動到最下方,在權限管理處,開啟外置存儲權限") .setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { finish(); } }) .setPositiveButton("確認", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // 跳轉到APP設置頁面 Intent intent = new Intent(); intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS); Uri uri = Uri.fromParts("package", getPackageName(), null); intent.setData(uri); startActivity(intent); } }) .create().show(); } } }
Android 敏感權限申請