PermissionUtils
import android.annotation.TargetApi; import android.app.Activity; import android.content.Context; import android.content.pm.PackageManager; import android.os.Build; import android.support.v4.content.ContextCompat; import java.util.ArrayList; import java.util.List; /** * DESC 6.0 权限工具类 * Created by douhaoqiang on 2017/2/27. */ public class PermissionUtils { private static final String TAG = "PermissionUtils"; private static int mRequestCode = -1; private static OnPermissionListener mOnPermissionListener; /** * 权限请求回调 */ public interface OnPermissionListener { //权限通过 void onPermissionGranted(); //权限拒绝 void onPermissionDenied(); } /** * 调用请求响应的权限 * @param context 上下文菜单 必须为Activity * @param requestCode 请求码 * @param permissions 请求权限 * @param listener 权限请求监听 */ @TargetApi(Build.VERSION_CODES.M) public static void requestPermissions(Context context, int requestCode , String[] permissions, OnPermissionListener listener) { if (context instanceof Activity) { mOnPermissionListener = listener; List<String> deniedPermissions = getDeniedPermissions(context, permissions); if (deniedPermissions.size() > 0) { mRequestCode = requestCode; ((Activity) context).requestPermissions(deniedPermissions .toArray(new String[deniedPermissions.size()]), requestCode); } else { if (mOnPermissionListener != null) mOnPermissionListener.onPermissionGranted(); } } else { throw new RuntimeException("Context must be an Activity"); } } /** * 获取请求权限中需要授权的权限 */ private static List<String> getDeniedPermissions(Context context, String... permissions) { List<String> deniedPermissions = new ArrayList<>(); for (String permission : permissions) { if (ContextCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_DENIED) { deniedPermissions.add(permission); } } return deniedPermissions; } /** * 请求权限结果,对应Activity中onRequestPermissionsResult()方法。 */ public static void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { if (mRequestCode != -1 && requestCode == mRequestCode) { if (mOnPermissionListener != null) { if (verifyPermissions(grantResults)) { mOnPermissionListener.onPermissionGranted(); } else { mOnPermissionListener.onPermissionDenied(); } } } } /** * 验证所有权限是否都已经授权 */ private static boolean verifyPermissions(int[] grantResults) { for (int grantResult : grantResults) { if (grantResult != PackageManager.PERMISSION_GRANTED) { return false; } } return true; } }
使用:
public class SplashActivity extends Activity { String[] permissions = { Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.CAMERA, Manifest.permission.RECORD_AUDIO }; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); //全屏 setContentView(R.layout.activity_splash); requestPermissions(); } public void requestPermissions() { PermissionUtil.requestPermissions(this, 1, permissions, new PermissionUtil.OnPermissionListener() { @Override public void onPermissionGranted() { startActivity(new Intent(SplashActivity.this, MainActivity.class)); finish(); } @Override public void onPermissionDenied() { finish(); } }); } @Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); PermissionUtil.onRequestPermissionsResult(requestCode, permissions, grantResults); } //隐藏虚拟导航栏按键 @Override public void onWindowFocusChanged(boolean hasFocus) { super.onWindowFocusChanged(hasFocus); if (hasFocus) { getWindow().getDecorView().setSystemUiVisibility( View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN ); } } }
最后,关注【码上加油站】微信公众号后,有疑惑有问题想加油的小伙伴可以码上加入社群,让我们一起码上加油吧!!!
posted on 2018-01-11 16:18 LoaderMan 阅读(1218) 评论(0) 编辑 收藏 举报