前言
之前写的app流程控制,正好在新项目上要用上,正好借机把当初的流程梳理一下,并改用AndPermission框架处理。
上一份用Visio化的流程图
之前采用的是和Activity的onRequestPermissionsResult事件
检查并申请
//参考代码
//private static final int MY_PERMISSIONS_REQUEST_CALL_CAMERA = 1;//请求码,自己定义//检查权限
int MY_PERMISSIONS_REQUEST_CALL_CAMERA = 1;//请求码,自己定义//检查权限
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
//如果没有授权,则请求授权
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, MY_PERMISSIONS_REQUEST_CALL_CAMERA);
} else {
//有授权,直接开启摄像头
startActivity(new Intent(getApplicationContext(), XZingMainActivity.class));
}
onRequestPermissionsResult授权回调
//Android 分多次(每次一个)请求权限时的onRequestPermissionsResult()方法
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode){
case 1:
switch (permissions[0]){
case Manifest.permission.READ_CONTACTS://权限1
if (grantResults.length > 0 && grantResults[0]==PackageManager.PERMISSION_GRANTED){
readContacts();
}else {
Toast.makeText(this, "You denied the permission", Toast.LENGTH_SHORT).show();
}
break;
case Manifest.permission.CALL_PHONE://权限2
if (grantResults.length > 0 && grantResults[0]==PackageManager.PERMISSION_GRANTED){
call();
} else {
Toast.makeText(this, "You denied the permission", Toast.LENGTH_SHORT).show();
}
break;
default:
}
break;
default:
}
}
github官网
https://github.com/yanzhenjie/AndPermission
第一步在gradle导入AndPermission
//权限管理
compile 'com.yanzhenjie:permission:2.0.0-rc4'
Welcome 界面代码
public class Welcome extends Activity {
private boolean islogined; //是否是第一次进入程序
private static final int TIME=2000; //handler等待的时间
private static final int GO_HOME=1000; //跳转到主activity消息
private static final int GO_GUIDE=1001;//跳转到导航activyt消息
private String[] PERMISSIONS_All_NEED = {
//文件读写权限
Manifest.permission.READ_EXTERNAL_STORAGE
,Manifest.permission.WRITE_EXTERNAL_STORAGE
//imei权限
,Manifest.permission.READ_PHONE_STATE
};
private String TAG= "AndPermissionHelper";
public void checkAllPermissions(final Context context){
AndPermission.with(context)
//.runtime()
.permission(PERMISSIONS_All_NEED)
//.onGranted(action)
.onGranted(new Action() {
@Override
public void onAction(List<String> list) {
// Storage permission are allowed.
init();
}
})
.onDenied(new Action() {
@Override
public void onAction(List<String> list) {
// Storage permission are not allowed.
//* 判断用户是否点击了禁止后不再询问
if (AndPermission.hasAlwaysDeniedPermission(context,PERMISSIONS_All_NEED)){
Toast.makeText(context, "部分功能被禁止,被禁止的功能将无法使用", Toast.LENGTH_SHORT).show();
Log.e(TAG,"部分功能被禁止");
showNormalDialog(Welcome.this);
//System.exit(0) ;//直接退出
}
}
})
.start();
}
private void showNormalDialog(final Context context){
final AlertDialog.Builder normalDialog =
new AlertDialog.Builder(context);
normalDialog.setIcon(R.drawable.appicon);
normalDialog.setTitle("去申请权限");
normalDialog.setMessage("部分权限被你禁止了,可能误操作,可能会影响部分功能,是否去要去重新设置?");
normalDialog.setPositiveButton("是",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//...To-do
getAppDetailSettingIntent(context);
}
});
normalDialog.setNegativeButton("否",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
init();
}
});
// 显示
normalDialog.show();
}
//以下代码可以跳转到应用详情,可以通过应用详情跳转到权限界面
static private void getAppDetailSettingIntent(Context context) {
Intent localIntent = new Intent();
localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (Build.VERSION.SDK_INT >= 9) {
localIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
localIntent.setData(Uri.fromParts("package", context.getPackageName(), null));
} else if (Build.VERSION.SDK_INT <= 8) {
localIntent.setAction(Intent.ACTION_VIEW);
localIntent.setClassName("com.android.settings","com.android.settings.InstalledAppDetails");
localIntent.putExtra("com.android.settings.ApplicationPkgName", context.getPackageName());
}
context.startActivity(localIntent);
}
private Handler mHanger= new Handler(){
//重写handle的Message方法
@Override
public void handleMessage(Message msg) {
//super.handleMessage(msg);
switch (msg.what){
case GO_HOME: //跳转到主界面
goHome();
break;
case GO_GUIDE://跳转到引导界面
goGuide();
break;
}
}
};
private void goGuide() {
Intent i=new Intent(Welcome.this,Guide.class);
startActivity(i);
finish();
}
private void goHome() {
Intent i=new Intent(Welcome.this,MainActivity.class);
startActivity(i);
finish();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome);
//init();
checkAllPermissions(this);
}
private void init(){
SharedPreferences sharedPreferences=getSharedPreferences("czg",MODE_PRIVATE);
islogined=sharedPreferences.getBoolean("islogined",true); //是否登录过
System.out.println("查看读取值"+islogined);
if (!islogined){
mHanger.sendEmptyMessageDelayed(GO_HOME,TIME);
}else {
mHanger.sendEmptyMessageDelayed(GO_GUIDE,TIME);
//把sharedPreferences保存起来,说明已经运行过一次
SharedPreferences.Editor editor=sharedPreferences.edit();
editor.putBoolean("islogined", false);
editor.commit();//提交修改
}
}
}