import android.Manifest;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;
/**
* 最标准的权限申请步奏,以拍照获取权限为案例,看效果可直接复制代码运行后看权限的申请步奏
* 在运行时请求权限:https://developer.android.com/training/permissions/requesting?hl=zh-cn
*/
public class MainActivity extends AppCompatActivity {
// 当前窗口
private Activity mActivity;
// 是否是首次拒绝
private boolean isPermissionCameraFirstRefuse;
// 首次拒绝相机权限申请的缓存变量key
private final static String KEY_PERMISSION_CAMERA_FIRST_REFUSE = "permission_camera_first_refuse";
// 相机权限首次申请的请求码
private final static int PERMISSION_CAMERA_REQUEST_CODE_FIRST = 1;
// 相机权限再次申请的请求码
private final static int PERMISSION_CAMERA_REQUEST_CODE_AGAIN = 2;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mActivity = this;
isPermissionCameraFirstRefuse = PermissionCacheUtil.getInstance().isFirst(Manifest.permission.CAMERA, true);
// 按钮点击事件,案例:申请拍照的权限
findViewById(R.id.btn_camera).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 权限申请
requestPermission(mActivity, Manifest.permission.CAMERA, "您需要先允许“相机”的授权才能进行拍照");
}
});
}
/**
* 权限申请
*
* @param activity
* @param permission
* @param message
*/
private void requestPermission(final Activity activity, final String permission, String message) {
if (ActivityCompat.checkSelfPermission(activity, permission) != PackageManager.PERMISSION_GRANTED) {
// 还没有权限
if (ActivityCompat.shouldShowRequestPermissionRationale(activity, permission)) {
// 非首次申请权限,应该先解释一下这个权限用来做什么
new AlertDialog.Builder(activity)
.setMessage(message)
.setPositiveButton("好的", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
ActivityCompat.requestPermissions(activity, new String[]{permission}, PERMISSION_CAMERA_REQUEST_CODE_AGAIN);
}
})
.create()
.show();
} else {
// 首次申请权限,或使用者已選取「不要再詢問」選項
ActivityCompat.requestPermissions(activity, new String[]{permission}, PERMISSION_CAMERA_REQUEST_CODE_FIRST);
}
} else {
// 已经有权限了
Toast.makeText(getApplicationContext(), "拍照权限申请成功了!", Toast.LENGTH_SHORT).show();
}
}
/**
* 权限结果回调
*
* @param requestCode
* @param permissions
* @param grantResults
*/
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case PERMISSION_CAMERA_REQUEST_CODE_FIRST:
if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// 首次申请权限,使用者就给了权限
Toast.makeText(getApplicationContext(), "拍照权限申请成功了!", Toast.LENGTH_SHORT).show();
} else if (isPermissionCameraFirstRefuse) {
// 首次拒绝权限,建议不做处理
isPermissionCameraFirstRefuse = false;
// 同时缓存变量也设置为false
PermissionCacheUtil.getInstance().setFirst(Manifest.permission.CAMERA, false);
} else {
// 首次拒绝给权限,或使用者已選取「不要再詢問」選項,需要做处理
new AlertDialog.Builder(mActivity)
.setMessage("拍照功能需要“相机”的权限,您可以在设置中启用该权限,是否前往应用程式设定界面?")
.setPositiveButton("前往设置", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Intent intent = new Intent();
intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", mActivity.getPackageName(), null);
intent.setData(uri);
mActivity.startActivity(intent);
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
})
.create()
.show();
}
break;
case PERMISSION_CAMERA_REQUEST_CODE_AGAIN:
if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// 经过解释后,再次请求权限,给了权限
Toast.makeText(getApplicationContext(), "拍照权限申请成功了!", Toast.LENGTH_SHORT).show();
} else {
// 经过解释后,再次请求权限,依然没有给权限
}
break;
}
}
}
import android.text.TextUtils;
public class PermissionCacheUtil {
private static PermissionCacheUtil instance;
private SharedPreferencesUtil spUtil;
private PermissionCacheUtil() {
spUtil = new SharedPreferencesUtil(BaseApplication.getInstance(), "PermissionCache");
}
public static PermissionCacheUtil getInstance() {
if (instance == null) {
instance = new PermissionCacheUtil();
}
return instance;
}
/**
* 设置首次访问权限
*
* @param permissionName
* @param isFirst
*/
public void setFirst(String permissionName, boolean isFirst) {
if (!TextUtils.isEmpty(permissionName)) {
spUtil.setBoolean(permissionName, isFirst);
}
}
/**
* 判断是否首次申请权限
*
* @param permissionName
* @param defaultValue
* @return
*/
public boolean isFirst(String permissionName, boolean defaultValue) {
if (!TextUtils.isEmpty(permissionName)) {
return spUtil.getBoolean(permissionName, defaultValue);
} else {
return defaultValue;
}
}
}
import android.content.Context;
import android.content.SharedPreferences;
import java.util.Map;
public class SharedPreferencesUtil {
private SharedPreferences sharedPref;
private SharedPreferences.Editor editor;
private final static int DEFAULT_CODE = -1;
@SuppressWarnings("deprecation")
public SharedPreferencesUtil(Context context, String name) {
sharedPref = context.getSharedPreferences(name,
Context.MODE_PRIVATE);
editor = sharedPref.edit();
}
public void setInt(String key, int value) {
if (sharedPref.contains(key)) {
editor.remove(key);
}
editor.putInt(key, value);
editor.commit();
}
public int getInt(String key) {
if (sharedPref.contains(key)) {
return sharedPref.getInt(key, DEFAULT_CODE);
} else {
return DEFAULT_CODE;
}
}
public int getInt(String key, int defaultValue) {
if (sharedPref.contains(key)) {
return sharedPref.getInt(key, defaultValue);
} else {
return defaultValue;
}
}
public void setFloat(String key, float value) {
if (sharedPref.contains(key)) {
editor.remove(key);
}
editor.putFloat(key, value);
editor.commit();
}
public float getFloat(String key) {
if (sharedPref.contains(key)) {
return sharedPref.getFloat(key, DEFAULT_CODE);
} else {
return DEFAULT_CODE;
}
}
public void setBoolean(String key, boolean value) {
if (sharedPref.contains(key)) {
editor.remove(key);
}
editor.putBoolean(key, value);
editor.commit();
}
public boolean getBoolean(String key) {
if (sharedPref.contains(key)) {
return sharedPref.getBoolean(key, false);
} else {
return false;
}
}
public boolean getBoolean(String key, boolean value) {
if (sharedPref.contains(key)) {
return sharedPref.getBoolean(key, value);
} else {
return value;
}
}
public void setLong(String key, long value) {
if (sharedPref.contains(key)) {
editor.remove(key);
}
editor.putLong(key, value);
editor.commit();
}
public long getLong(String key) {
if (sharedPref.contains(key)) {
return sharedPref.getLong(key, DEFAULT_CODE);
} else {
return DEFAULT_CODE;
}
}
public long getLong(String key, long value) {
if (sharedPref.contains(key)) {
return sharedPref.getLong(key, value);
} else {
return value;
}
}
public void setString(String key, String value) {
if (sharedPref.contains(key)) {
editor.remove(key);
}
editor.putString(key, value);
editor.commit();
}
public String getString(String key) {
if (sharedPref.contains(key)) {
return sharedPref.getString(key, "");
} else {
return "";
}
}
public String getString(String key, String value) {
if (sharedPref.contains(key)) {
return sharedPref.getString(key, value);
} else {
return value;
}
}
public Map<String, ?> getAll() {
return sharedPref.getAll();
}
public void remove(String key) {
if (sharedPref.contains(key)) {
editor.remove(key);
editor.commit();
}
}
public void clear() {
if (null != editor) {
editor.clear();
editor.commit();
}
}
}