android6.0如何封装运行时的权限
这篇文章将为大家详细讲解有关android6.0如何封装运行时的权限,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
创新互联公司是一家专注于网站设计、做网站与策划设计,奎文网站建设哪家好?创新互联公司做网站,专注于网站建设10余年,网设计领域的专业建站公司;建站业务涵盖:奎文等地区。奎文做网站价格咨询:18980820575
示例
在这里我进行了简单的在activity中简单进行获取权限和工具类封:
某一个权限被禁止或者是所有的权限被禁止,这里我做了去到烯烃界面去设置的处理:
如果要开启多个权限的话,则可能不同的手机显示的效果不同,有些手机在权限Dialog上显示需要获取权限的个数,但有些手机需要多少个权限显示多少个。
最后一步处理:如果某个权限被禁止的话,则到设置界面去提示用户去打开权限:
部分代码:
/** * 类功能描述:在activity中进行检测权限的简单方法 * 博客地址:http://blog.csdn.net/androidstarjack * @author 老于 * @version 1.0 修改时间: 修改备注: */ 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; } ListneetRequestPermisList = new ArrayList (); 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 = "当前应用缺少必要权限。
\r请点击\"设置\"-\"权限\"-打开所需权限。
\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运行时权限 * permission权限控制器 * 博客地址:http://blog.csdn.net/androidstarjack * @author 老于 * Created on 2017/1/3/002 * @version 1.0 修改时间: 修改备注: */ 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如何封装运行时的权限”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。
本文标题:android6.0如何封装运行时的权限
网站链接:http://ybzwz.com/article/pcgdjo.html