Android6.0
全称:Marshmallow
主要更新:电源管理(应用待机)、App link(应用之间交互)、指纹识别、应用程序权限管理等等
Android6.0权限主要分为两类Normal Permissions和Dangerous Permission
我们可以通过adb shell pm list permissions -d -g查看危险权限组
对于危险权限我们需要在程序中进行判断,具体实现代码如下:
一、自己封装权限管理模块
package com.czhappy.permissiontest.utils;
/**
* Description:
* User: chenzheng
* Date: 2016/12/14 0014
* Time: 15:10
*/
public class Constants {
public static final int WRITE_EXTERNAL_CODE = 0X01;
public static final int CALL_PHONE_CODE = 0X02;
}
package com.czhappy.permissiontest.activity;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import com.czhappy.permissiontest.utils.Constants;
/**
* Description:
* User: chenzheng
* Date: 2016/12/14 0014
* Time: 15:03
*/
public class BaseActivity extends AppCompatActivity {
@Override
public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
super.onCreate(savedInstanceState, persistentState);
}
public boolean hasPermission(String... permissions){
for(String permission : permissions){
if(ContextCompat.checkSelfPermission(this, permission)!= PackageManager.PERMISSION_GRANTED){
return false;
}
}
return true;
}
public void requestPermission(int code, String... permissions){
ActivityCompat.requestPermissions(this, permissions, code);
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch(requestCode){
case Constants.WRITE_EXTERNAL_CODE:
if(grantResults.length>0 && grantResults[0]==PackageManager.PERMISSION_GRANTED){
doSdCardPermission();
}else{
Toast.makeText(BaseActivity.this, "权限被拒绝了", Toast.LENGTH_LONG).show();
}
break;
case Constants.CALL_PHONE_CODE:
if(grantResults.length>0 && grantResults[0]==PackageManager.PERMISSION_GRANTED){
doPhoneCall();
}else{
Toast.makeText(BaseActivity.this, "权限被拒绝了", Toast.LENGTH_LONG).show();
}
break;
}
}
/**
* 默认的sdcard处理权限
*/
public void doSdCardPermission(){
}
/**
* 默认的打电话权限
*/
public void doPhoneCall(){
}
}
package com.czhappy.permissiontest.activity;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import com.czhappy.permissiontest.R;
import com.czhappy.permissiontest.utils.Constants;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.FileCallback;
import java.io.File;
import okhttp3.Call;
import okhttp3.Response;
public class MainActivity extends BaseActivity implements View.OnClickListener {
private Button phonecall_btn, download_btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
phonecall_btn = (Button) this.findViewById(R.id.phonecall_btn);
phonecall_btn.setOnClickListener(this);
download_btn = (Button) this.findViewById(R.id.download_btn);
download_btn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.phonecall_btn://拨打电话
goPhoneCall();
break;
case R.id.download_btn://文件下载
sdCardPermission();
break;
}
}
private void sdCardPermission() {
if (hasPermission(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE})) {
doSdCardPermission();
} else {
requestPermission(Constants.WRITE_EXTERNAL_CODE, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE});
}
}
@Override
public void doSdCardPermission() {
OkGo.g
.tag(this)
.execute(new FileCallback(System.currentTimeMillis() + ".jpg") {
@Override
public void onSuccess(File file, Call call, Response response) {
Log.e("tag", file.getAbsolutePath());
}
});
}
private void goPhoneCall() {
if (hasPermission(new String[]{Manifest.permission.CALL_PHONE})) {
doPhoneCall();
} else {
requestPermission(Constants.CALL_PHONE_CODE, new String[]{Manifest.permission.CALL_PHONE});
}
}
@Override
public void doPhoneCall() {
Intent intent = new Intent(Intent.ACTION_CALL);
Uri data = Uri.parse("tel:" + "10086");
intent.setData(data);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
return;
}
this.startActivity(intent);
}
}
二、使用开源库permissionGen实现
github地址:https://github.com/lovedise/PermissionGen
package com.czhappy.permissiontest.activity;
import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import com.czhappy.permissiontest.R;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.FileCallback;
import java.io.File;
import kr.co.namee.permissiongen.PermissionGen;
import kr.co.namee.permissiongen.PermissionSuccess;
import okhttp3.Call;
import okhttp3.Response;
public class MainActivity extends Activity implements View.OnClickListener {
private Button phonecall_btn, download_btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
phonecall_btn = (Button) this.findViewById(R.id.phonecall_btn);
phonecall_btn.setOnClickListener(this);
download_btn = (Button) this.findViewById(R.id.download_btn);
download_btn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.phonecall_btn://拨打电话
goPhoneCall();
break;
case R.id.download_btn://文件下载
sdCardPermission();
break;
}
}
private void sdCardPermission() {
PermissionGen.needPermission(this, 100,
new String[] {
Manifest.permission.WRITE_EXTERNAL_STORAGE
}
);
}
@PermissionSuccess(requestCode = 200)
public void doSdCardPermission() {
OkGo.get(
.tag(this)
.execute(new FileCallback(System.currentTimeMillis() + ".jpg") {
@Override
public void onSuccess(File file, Call call, Response response) {
Log.e("tag", file.getAbsolutePath());
}
});
}
private void goPhoneCall() {
PermissionGen.needPermission(this, 100,
new String[] {
Manifest.permission.CALL_PHONE
}
);
}
@PermissionSuccess(requestCode = 100)
public void doPhoneCall() {
Intent intent = new Intent(Intent.ACTION_CALL);
Uri data = Uri.parse("tel:" + "10086");
intent.setData(data);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
return;
}
this.startActivity(intent);
}
@Override public void onRequestPermissionsResult(int requestCode, String[] permissions,
int[] grantResults) {
PermissionGen.onRequestPermissionsResult(this, requestCode, permissions, grantResults);
}
}
三、基于BaseActivity运行时权限封装
PermissionListener.java
package com.czhappy.permissiontest.utils;
import java.util.List;
/**
* Description:
* User: chenzheng
* Date: 2016/12/31 0031
* Time: 14:53
*/
public interface PermissionListener {
void onGranted();
void onDenied(ListdeniedPermission);
}
ActivityCollector.java
package com.czhappy.permissiontest.utils;
import android.app.Activity;
import java.util.ArrayList;
import java.util.List;
/**
* Description:
* User: chenzheng
* Date: 2016/12/31 0031
* Time: 15:14
*/
public class ActivityCollector {
public static List activityList = new ArrayList();
public static void addActivity(Activity activity){
activityList.add(activity);
}
public static void removeActivity(Activity activity){
activityList.remove(activity);
}
public static Activity getTopActivity(){
if(activityList.isEmpty()){
return null;
}else{
//获取栈顶Activity
return activityList.get(activityList.size()-1);
}
}
}
BasePermissionsActivity.java
package com.czhappy.permissiontest.activity;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import com.czhappy.permissiontest.utils.ActivityCollector;
import com.czhappy.permissiontest.utils.PermissionListener;
import java.util.ArrayList;
import java.util.List;
/**
* Description:
* User: chenzheng
* Date: 2016/12/31 0031
* Time: 14:41
*/
public class BasePermissionsActivity extends AppCompatActivity {
private static PermissionListener mListener;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityCollector.addActivity(this);
}
public static void requestRuntimePermission(String[] permissions, PermissionListener listener) {
Activity topActivity = ActivityCollector.getTopActivity();
if(topActivity==null){
return;
}
mListener = listener;
ListpermissionList = new ArrayList ();
for (String permission : permissions) {
if (ContextCompat.checkSelfPermission(topActivity, permission) != PackageManager.PERMISSION_GRANTED) {
permissionList.add(permission);
}
}
if (!permissionList.isEmpty()) {
ActivityCompat.requestPermissions(topActivity, permissionList.toArray(new String[permissionList.size()]), 1);
} else {
mListener.onGranted();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case 1:
if (grantResults.length > 0) {
ListdeniedPermissions = new ArrayList ();
for (int i = 0; i int grantResult = grantResults[i];
String permission = permissions[i];
if (grantResult != PackageManager.PERMISSION_GRANTED) {
deniedPermissions.add(permission);
}
}
if (deniedPermissions.isEmpty()) {
mListener.onGranted();
} else {
mListener.onDenied(deniedPermissions);
}
}
break;
default:
break;
}
}
@Override
protected void onDestroy() {
super.onDestroy();
ActivityCollector.removeActivity(this);
}
}