一,创建工具类
0 1 2import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresApi;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class PermissionUtils {
private ActivitymActivity;
private int mReqCode;
private CallBackmCallBack;
public static interface CallBack {
void grantAll();
void denied();
}
public PermissionUtils(Activity activity) {
mActivity = activity;
}
@RequiresApi(api = Build.VERSION_CODES.M)
public void request(List needPermissions, int reqCode, CallBack callback) {
if (Build.VERSION.SDK_INT <23) {
callback.grantAll();
return;
}
if (mActivity ==null) {
throw new IllegalArgumentException("activity is null.");
}
mReqCode = reqCode;
mCallBack = callback;
List reqPermissions =new ArrayList<>();
for (String permission : needPermissions) {
if (mActivity.checkSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) {
reqPermissions.add(permission);
}
}
// fixed!!!
if (reqPermissions.isEmpty()) {
callback.grantAll();
return;
}
mActivity.requestPermissions(reqPermissions.toArray(new String[]{}), reqCode);
}
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode ==mReqCode) {
boolean grantAll =true;
for (int i =0; i < grantResults.length; i++) {
if (grantResults[i] != PackageManager.PERMISSION_GRANTED) {
grantAll =false;
Toast.makeText(mActivity, permissions[i] +" 未授权", Toast.LENGTH_SHORT).show();
break;
}
}
if (grantAll) {
mCallBack.grantAll();
}else {
mCallBack.denied();
}
}
}
}
二 ,Activity中调用该工具类
3 4import android.Manifest;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import java.util.ArrayList;
import java.util.List;
public class SplashActivity extends AppCompatActivity {
public static List<String> NeedReqPermissions =new ArrayList<>();
static {
sNeedReqPermissions.add(Manifest.permission.READ_PHONE_STATE);
sNeedReqPermissions.add(Manifest.permission.ACCESS_COARSE_LOCATION);
sNeedReqPermissions.add(Manifest.permission.ACCESS_FINE_LOCATION);
sNeedReqPermissions.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
}
private PermissionUtilsmPermissionUtils;
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
mPermissionUtils =new PermissionUtils(this);
mPermissionUtils.request(sNeedReqPermissions, 100, new PermissionUtils.CallBack() {
@Override
public void grantAll() {
toMainActivity();
finish();
}
@Override
public void denied() {
finish();
}
});
}
private void toMainActivity() {
Intent intent =new Intent(this, MainActivity.class);
startActivity(intent);
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
mPermissionUtils.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
网友评论