您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關android6.0如何封裝運行時的權限,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
示例
在這里我進行了簡單的在activity中簡單進行獲取權限和工具類封:
某一個權限被禁止或者是所有的權限被禁止,這里我做了去到烯烴界面去設置的處理:
如果要開啟多個權限的話,則可能不同的手機顯示的效果不同,有些手機在權限Dialog上顯示需要獲取權限的個數,但有些手機需要多少個權限顯示多少個。
最后一步處理:如果某個權限被禁止的話,則到設置界面去提示用戶去打開權限:
部分代碼:
/** * 類功能描述:在activity中進行檢測權限的簡單方法 </br> * 博客地址:http://blog.csdn.net/androidstarjack * @author 老于 * @version 1.0 </p> 修改時間:</br> 修改備注:</br> */ public class CheckPermissionOnActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_checkpermission1); ButterKnife.bind(this); } @OnClick({R.id.btn_runtimne_onClick, R.id.btn_runtimne_onClick_multify,R.id.btn_runtimne_onClick_seting}) public void onMyListener(View v){ switch (v.getId()){ case R.id.btn_runtimne_onClick: requestToUserPersiom(1); break; case R.id.btn_runtimne_onClick_multify: requestToUserPersiom(2); break; case R.id.btn_runtimne_onClick_seting: Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS); intent.setData(Uri.parse("package:"+ getPackageName())); startActivity(intent); break; } } public void requestToUserPersiom(int permisionIndexCount){ switch (permisionIndexCount){ case 1: ActivityCompat.requestPermissions(CheckPermissionOnActivity.this, new String[]{android.Manifest.permission.CALL_PHONE},100);//去請求權限 break; case 2: case 3: case 4: ActivityCompat.requestPermissions(CheckPermissionOnActivity.this, new String[]{android.Manifest.permission.CALL_PHONE,android.Manifest.permission.WRITE_EXTERNAL_STORAGE},100); break; } } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); if(requestCode == 100){ if(grantResults.length >0 ){ for (int i = 0; i < grantResults.length; i++) { if( grantResults[i] == PackageManager.PERMISSION_DENIED){//拒絕啦 return ; } } doosmething(); } } } /** * 做其他事情 */ public void doosmething(){ GetToast.useString(CheckPermissionOnActivity.this,"開始做其他事情了"); } }
封裝成BaseActivitgy工具類:
public class BasePermisitionActivity extends Activity { private static final int REQUESTPERMISSION_CODE = 100; private static QuestPermissionListener questPermissionListener; private AlertDialog.Builder builder; private AlertDialog alertDialog; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ActivityManager.addPermissionActivty(this); } public static void requestRuntimePermission(String [] permissions,QuestPermissionListener questPermissionListener){ Activity actyivty = ActivityManager.permissionActivilyList.get(ActivityManager.permissionActivilyList.size() -1); if(actyivty == null){ return; } List<String> neetRequestPermisList = new ArrayList<String>(); for (String permission : permissions) { if(ActivityCompat.checkSelfPermission(actyivty,permission) == PackageManager.PERMISSION_DENIED){ neetRequestPermisList.add(permission); } } BasePermisitionActivity.questPermissionListener = questPermissionListener; if(!neetRequestPermisList.isEmpty()){ ActivityCompat.requestPermissions(actyivty,neetRequestPermisList.toArray(new String[neetRequestPermisList.size()]),REQUESTPERMISSION_CODE); } } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); if(requestCode == REQUESTPERMISSION_CODE){ for (int i = 0; i < grantResults.length; i++) { int granrRequest = grantResults[i]; if(granrRequest == PackageManager.PERMISSION_DENIED){//權限被拒絕啦 if(questPermissionListener != null){ questPermissionListener.denySomePermission(); } return; } } questPermissionListener.doAllPermissionGrant(); } } @Override protected void onResume() { super.onResume(); } public void onResumeCheckPermission(String ...permission){ if(PermisionControl.lackMissPermission(this,permission)){ showMissPermissionDialog(); } } @Override protected void onDestroy() { super.onDestroy(); ActivityManager.removePermissionActiivty(this); } /** * 丟失權限,進行彈框設置 */ public void showMissPermissionDialog() { if (builder == null) { builder = new AlertDialog.Builder(BasePermisitionActivity.this); builder.setTitle("幫助"); String msg = "當前應用缺少必要權限。<br>\r請點擊\"設置\"-\"權限\"-打開所需權限。<br>\r\r最后點擊兩次后退按鈕,即可返回。"; builder.setMessage(Html.fromHtml(msg)); //materialDialog.setMovementMethod(LinkMovementMethod.getInstance()); builder.setPositiveButton("設置", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { alertDialog.dismiss(); } }); builder.setNegativeButton("退出", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { alertDialog.dismiss(); startAppSettings(); } }); alertDialog = builder.create(); alertDialog.show(); } else { alertDialog.show(); } } // 啟動應用的設置 public void startAppSettings() { try { Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS); intent.setData(Uri.parse("package:"+ getPackageName())); startActivity(intent); } catch (Exception e) { e.printStackTrace(); } } }
與其相關的工具類:
/** * 類功能描述:6.0運行時權限 </br> * permission權限控制器</br> * 博客地址:http://blog.csdn.net/androidstarjack * @author 老于 * Created on 2017/1/3/002 * @version 1.0 </p> 修改時間:</br> 修改備注:</br> */ public class PermisionControl { /** * 檢查所有的權限是否被禁止 */ public static boolean lackMissPermission(Context cnt ,String... permission){ boolean relust = true ; for (String per : permission) { if(ActivityCompat.checkSelfPermission(cnt,per) == PackageManager.PERMISSION_DENIED){ relust = false ; break; } } return relust; } /** * 請求運行時權限 * eg: */ public void requestRuntimePermission(QuestPermissionListener questPermissionListener, String... permissions){ BasePermisitionActivity.requestRuntimePermission(permissions,questPermissionListener); } }
以及其他的一些封裝:
public class PermissionUsage { /**********權限:打電話,寫入內存卡,定位********/ public static String[] LOACATION_CAMERA = { Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.CALL_PHONE, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.READ_EXTERNAL_STORAGE}; /**********權限:讀取內存卡********/ public static String[] READ_EXTRASORE = { Manifest.permission.READ_EXTERNAL_STORAGE}; /**********權限:打電話權限********/ public static String[] RCALL_PHONE = { Manifest.permission.READ_EXTERNAL_STORAGE}; }
關于“android6.0如何封裝運行時的權限”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。