Android 6.0 要单独的获取权限
这里提供两种很简单的方法
- EasyPermition
- RxPermition
EasyPermition
https://github.com/googlesamples/easypermissions
dependencies {
//6.0 permition
compile 'pub.devrel:easypermissions:0.1.7'
}
在启动的Activity添加如下代码
public class LoginActivity extends Activity implements EasyPermissions.PermissionCallbacks {
private static final int RC_CAMERA_PERM = ;
private static final int RC_LOCATION_CONTACTS_PERM = ; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
requestPermissions();
} private void requestPermissions() {
//要获取的权限
String[] perms = {Manifest.permission.INTERNET, Manifest.permission.WRITE_EXTERNAL_STORAGE};
if (EasyPermissions.hasPermissions(this, perms)) {
// Have permissions, do the thing!
Toast.makeText(this, "TODO: Location and Contacts things", Toast.LENGTH_LONG).show();
} else {
// Ask for both permissions
EasyPermissions.requestPermissions(this, getString(R.string.rationale_location_contacts),
RC_LOCATION_CONTACTS_PERM, perms);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
} @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) {
Log.d(TAG, "onPermissionsDenied:" + requestCode + ":" + perms.size());
EasyPermissions.checkDeniedPermissionsNeverAskAgain(this,
getString(R.string.rationale_ask_again),
R.string.setting, R.string.cancel, perms);
}
}
RxPermition
https://github.com/tbruyelle/RxPermissions
dependencies {
compile 'com.tbruyelle.rxpermissions:rxpermissions:0.7.0@aar'
}
同样在启动的Activity
public class LoginActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login); Observable<Boolean> request = RxPermissions.getInstance(Global.getContext())
.request(
Manifest.permission.WRITE_EXTERNAL_STORAGE//文件读写
/* Manifest.permission.READ_CONTACTS,//联系人
Manifest.permission.READ_PHONE_STATE,
Manifest.permission.ACCESS_FINE_LOCATION*/
);
request.subscribe(new Action1<Boolean>() {
@Override
public void call(Boolean granted) {
if (granted) {
startActivity(new Intent(LoginActivity.this, MainActivity.class));
}
}
});
}
}
针对RxPermition、写了个工具类 大家可以参考如下
public class PermissionsUtils { public static Observable<Boolean> getFileAndContactsAndLocationGrant() {
Observable<Boolean> request = RxPermissions.getInstance(Global.getContext())
.request(
Manifest.permission.WRITE_EXTERNAL_STORAGE,//文件读写
Manifest.permission.READ_CONTACTS,//联系人
Manifest.permission.READ_PHONE_STATE,
Manifest.permission.ACCESS_FINE_LOCATION
);
return request;
} public static Observable<Boolean> getCameraAndAudioGrant() {
Observable<Boolean> request = RxPermissions.getInstance(Global.getContext())
.request(
Manifest.permission.CAMERA,//摄像头
Manifest.permission.RECORD_AUDIO//麦克风
);
return request;
}
/**
* 引导到设置界面
* @param activity
* @param isFinish
*/
@MainThread
public static void showPermissionDeniedDialog(final Activity activity, final boolean isFinish) {
DialogDeleteFriend dialog = new DialogDeleteFriend(activity, R.style.MyDialogBg);
dialog.show();
dialog.setIBtnListener(new IBtnClickListener() {
@Override
public void btnOk() {
Intent intent = new Intent(Settings.ACTION_MANAGE_APPLICATIONS_SETTINGS);
activity.startActivity(intent);
if (isFinish) activity.finish();
} @Override
public void btnCancle() {
if (isFinish) activity.finish();
}
});
dialog.updateType(DialogDeleteFriend.TYPE_TO_GRANTED);
}
}
用法如下
/**
* 获取授权后加载登录信息,并进入登录界面 如果没获取到权限则引导到设置页面
*/
private void getGrantedAndInitLoginInfo() {
rx.Observable<Boolean> grantObservable = PermissionsUtils.getFileAndContactsAndLocationGrant();
grantObservable.subscribe(new Action1<Boolean>() {
@Override
public void call(Boolean granted) {
if (granted) {
//do somthing 6.0以后需要权限
} else {
PermissionsUtils.showPermissionDeniedDialog(SplashActivity.this, true);
}
}
});
}