方法一:
public void method(){ if (!checkPermission(activity, permission.ACCESS_COARSE_LOCATION)) { Toast.makeText(activity, "定位权限关闭,无法获取地理位置", Toast.LENGTH_SHORT).show(); } } private boolean checkPermission(Context context, permission permName) { int perm = context.checkCallingOrSelfPermission("android.permission."+permName.toString()); return perm == PackageManager.PERMISSION_GRANTED; } private enum permission{ ACCESS_COARSE_LOCATION, ACCESS_FINE_LOCATION }
方法一: private String[] permissions = {Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION}; int i = ContextCompat.checkSelfPermission(getApplicationContext(), permissions[0]); if (i != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(this, permissions, 321); } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); if (requestCode == 321) { Log.d("weijie", "onRequestPermissionsResult: "); getLocation(); } }