android 6 系统警报权限..!
android 6 system alert permission..!
我为 android 6 提供了几个运行时权限,但在每次重新启动设备或单击服务启动时,都会出现一个对话框,系统警报和读取联系人权限。
我的应用程序在后台工作(作为重启后广播调用的服务)这是第一个权限,我注意到上面没有永不询问选项
这是我说的第二个系统警报
现在
我如何才能授予一次权限,再也不会询问我已经在检查权限,但这是不同的事情,它仅用于读取联系人权限
这已经在 Android android.developer.com
的文档中
Android发出请求的方式取决于系统版本,以及您的应用程序针对的系统版本:
如果设备是 运行ning Android 6.0(API 23 级)或更高,并且应用的 targetSdkVersion 为 23 或更高,应用在 运行 时向用户请求权限。用户可以随时撤销权限,所以app每次访问受权限保护的API都需要检查自己是否有权限。有关在您的应用中请求权限的更多信息,请参阅使用系统权限培训指南。
您可能设置了 app's targetSdkVersion is 22 or lower
如前所述:
如果设备是 运行ning Android 5.1.1(API 22 级)或更低版本,或者应用的 targetSdkVersion 是 22 或更低版本,当用户安装应用程序时,系统会要求用户授予权限。如果您向应用程序的更新版本添加新权限,系统会在用户更新应用程序时要求用户授予该权限。一旦用户安装了该应用程序,他们撤销权限的唯一方法就是卸载该应用程序。
您也可以使用我的自定义权限 Class
import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.StrictMode;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import java.lang.reflect.Method;
public class PermissionUtil {
private static final String TAG = PermissionUtil.class.getSimpleName();
public static final int REQUEST_EXTERNAL_STORAGE = 1;
private static final int WRITE_EXTERNAL_PERMISSION_REQUEST_CODE = 1;
public static final int READ_EXTERNAL_PERMISSION_REQUEST_CODE = 2;
public static final int RECORD_AUDIO_PERMISSION_REQUEST_CODE = 3;
public static final int CAMERA_PERMISSION_REQUEST_CODE = 4;
private static String[] PERMISSIONS_WRITE_STORAGE = {Manifest.permission.WRITE_EXTERNAL_STORAGE};
private static String[] PERMISSIONS_READ_STORAGE = {Manifest.permission.READ_EXTERNAL_STORAGE};
private static String[] PERMISSIONS_AUDIO = {Manifest.permission.RECORD_AUDIO};
private static String[] PERMISSIONS_CAMERA = {Manifest.permission.CAMERA};
PermissionUtil() { }
/**
* SAMPLER
* Checks if the app has permission to write to device storage
* <p/>
* If the app does not has permission then the user will be prompted to grant permissions
*
* @param activity the mContext from which permissions are checked
*/
public static void verifyStoragePermissions(Activity activity) {
// Check if we have write permission
int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (permission != PackageManager.PERMISSION_GRANTED) {
// We don't have permission so prompt the user
ActivityCompat.requestPermissions(
activity,
PERMISSIONS_WRITE_STORAGE,
WRITE_EXTERNAL_PERMISSION_REQUEST_CODE
);
}
}
public static void verrifyReadStoragePermissions(Activity activity) {
int permission = ActivityCompat.checkSelfPermission(activity, PERMISSIONS_READ_STORAGE[0]);
if (isPermissionDenied(permission)) {
processPermission(activity, PERMISSIONS_READ_STORAGE[0], PERMISSIONS_READ_STORAGE, READ_EXTERNAL_PERMISSION_REQUEST_CODE);
}
}
public static void verrifyWriteStoragePermissions(Activity activity) {
int permission = ActivityCompat.checkSelfPermission(activity, PERMISSIONS_WRITE_STORAGE[0]);
if (isPermissionDenied(permission)) {
processPermission(activity, PERMISSIONS_WRITE_STORAGE[0], PERMISSIONS_WRITE_STORAGE, WRITE_EXTERNAL_PERMISSION_REQUEST_CODE);
}
}
public static void verrifyRecordAudioPermissions(Activity activity) {
int permission = ActivityCompat.checkSelfPermission(activity, PERMISSIONS_AUDIO[0]);
if (isPermissionDenied(permission)) {
processPermission(activity, PERMISSIONS_AUDIO[0], PERMISSIONS_AUDIO, RECORD_AUDIO_PERMISSION_REQUEST_CODE);
}
}
public static void verrifyCameraPermissions(Activity activity) {
int permission = ActivityCompat.checkSelfPermission(activity, PERMISSIONS_CAMERA[0]);
if (isPermissionDenied(permission)) {
processPermission(activity, PERMISSIONS_CAMERA[0], PERMISSIONS_CAMERA, CAMERA_PERMISSION_REQUEST_CODE);
}
}
private static boolean isPermissionDenied(int permission) {
return permission != PackageManager.PERMISSION_GRANTED;
}
private static void processPermission(Activity activity, String permissionManifest, String[] permissions, int requestCode) {
if (ActivityCompat.shouldShowRequestPermissionRationale(activity, permissionManifest)) {
Log.e(TAG, "shouldShowRequestPermissionRationale is invoked " + permissionManifest);
} else {
ActivityCompat.requestPermissions(activity, permissions, requestCode);
Log.e(TAG, "requestPermissions is invoked " + permissionManifest);
}
}
/**
* SAMPLER
* Request the permissions you need
If your app doesn't already have the permission it needs, the app must call one of the requestPermissions() methods to request the appropriate permissions.
Your app passes the permissions it wants, and also an integer request code that you specify to identify this permission request.
This method functions asynchronously: it returns right away, and after the user responds to the dialog box,
the system calls the app's callback method with the results, passing the same request code that the app passed to requestPermissions().
* @param activity - you mContext
*/
public static void verifyShowRequestPrompt(Activity activity) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(activity,
Manifest.permission.CAMERA)) {
// Show an explanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
Log.e(TAG, "shouldShowRequestPermissionRationale is invoked");
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(activity,
new String[]{Manifest.permission.CAMERA},
WRITE_EXTERNAL_PERMISSION_REQUEST_CODE);
// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
// app-defined int constant. The callback method gets the
// result of the request.
}
}
/**
* ABOVE CODES is not used
* @param activity
*/
public static void initPermissions(final Activity activity) {
// The request code used in ActivityCompat.requestPermissions()
// and returned in the Activity's onRequestPermissionsResult()
// int PERMISSION_ALL = 1;
final String[] PERMISSIONS = {
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.RECORD_AUDIO,
Manifest.permission.CAMERA};
if(!hasPermissions(activity, PERMISSIONS)) {
showMessageOKCancel(activity, "These permissions are mandatory for the application. Please allow access.",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (!hasPermissions(activity, PERMISSIONS)) {
ActivityCompat.requestPermissions(activity, PERMISSIONS, REQUEST_EXTERNAL_STORAGE);
}
}
});
}
setGAlleryPermissionIntent();
}
public static void initPermissions(final Context context) {
// The request code used in ActivityCompat.requestPermissions()
// and returned in the Activity's onRequestPermissionsResult()
// int PERMISSION_ALL = 1;
final String[] PERMISSIONS = {
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.RECORD_AUDIO,
Manifest.permission.CAMERA};
if(!hasPermissions(context, PERMISSIONS)) {
showMessageOKCancel(context, "These permissions are mandatory for the application. Please allow access.",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (!hasPermissions(context, PERMISSIONS)) {
ActivityCompat.requestPermissions((Activity) context, PERMISSIONS, REQUEST_EXTERNAL_STORAGE);
}
}
});
}
setGAlleryPermissionIntent();
}
public static boolean hasPermissions(final Context context, final String... permissions) {
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null && permissions != null) {
for (String permission : permissions) {
if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
}
return true;
}
private static void showMessageOKCancel(Context context, String message, DialogInterface.OnClickListener okListener) {
new AlertDialog.Builder(context)
.setMessage(message)
.setPositiveButton("OK", okListener)
.setNegativeButton("Cancel", null)
.create()
.show();
}
public static void setGAlleryPermissionIntent() {
if(Build.VERSION.SDK_INT>=24){
try{
Method m = StrictMode.class.getMethod("disableDeathOnFileUriExposure");
m.invoke(null);
}catch(Exception e){
e.printStackTrace();
}
}
}
}
只需在您的 activity 上调用它即可。
void onCreate(...) {
PermissionUtil.verrifyReadStoragePermissions(this);
...
}
我为 android 6 提供了几个运行时权限,但在每次重新启动设备或单击服务启动时,都会出现一个对话框,系统警报和读取联系人权限。
我的应用程序在后台工作(作为重启后广播调用的服务)这是第一个权限,我注意到上面没有永不询问选项
这是我说的第二个系统警报
现在 我如何才能授予一次权限,再也不会询问我已经在检查权限,但这是不同的事情,它仅用于读取联系人权限
这已经在 Android android.developer.com
的文档中Android发出请求的方式取决于系统版本,以及您的应用程序针对的系统版本:
如果设备是 运行ning Android 6.0(API 23 级)或更高,并且应用的 targetSdkVersion 为 23 或更高,应用在 运行 时向用户请求权限。用户可以随时撤销权限,所以app每次访问受权限保护的API都需要检查自己是否有权限。有关在您的应用中请求权限的更多信息,请参阅使用系统权限培训指南。
您可能设置了 app's targetSdkVersion is 22 or lower
如前所述:
如果设备是 运行ning Android 5.1.1(API 22 级)或更低版本,或者应用的 targetSdkVersion 是 22 或更低版本,当用户安装应用程序时,系统会要求用户授予权限。如果您向应用程序的更新版本添加新权限,系统会在用户更新应用程序时要求用户授予该权限。一旦用户安装了该应用程序,他们撤销权限的唯一方法就是卸载该应用程序。
您也可以使用我的自定义权限 Class
import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.StrictMode;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import java.lang.reflect.Method;
public class PermissionUtil {
private static final String TAG = PermissionUtil.class.getSimpleName();
public static final int REQUEST_EXTERNAL_STORAGE = 1;
private static final int WRITE_EXTERNAL_PERMISSION_REQUEST_CODE = 1;
public static final int READ_EXTERNAL_PERMISSION_REQUEST_CODE = 2;
public static final int RECORD_AUDIO_PERMISSION_REQUEST_CODE = 3;
public static final int CAMERA_PERMISSION_REQUEST_CODE = 4;
private static String[] PERMISSIONS_WRITE_STORAGE = {Manifest.permission.WRITE_EXTERNAL_STORAGE};
private static String[] PERMISSIONS_READ_STORAGE = {Manifest.permission.READ_EXTERNAL_STORAGE};
private static String[] PERMISSIONS_AUDIO = {Manifest.permission.RECORD_AUDIO};
private static String[] PERMISSIONS_CAMERA = {Manifest.permission.CAMERA};
PermissionUtil() { }
/**
* SAMPLER
* Checks if the app has permission to write to device storage
* <p/>
* If the app does not has permission then the user will be prompted to grant permissions
*
* @param activity the mContext from which permissions are checked
*/
public static void verifyStoragePermissions(Activity activity) {
// Check if we have write permission
int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (permission != PackageManager.PERMISSION_GRANTED) {
// We don't have permission so prompt the user
ActivityCompat.requestPermissions(
activity,
PERMISSIONS_WRITE_STORAGE,
WRITE_EXTERNAL_PERMISSION_REQUEST_CODE
);
}
}
public static void verrifyReadStoragePermissions(Activity activity) {
int permission = ActivityCompat.checkSelfPermission(activity, PERMISSIONS_READ_STORAGE[0]);
if (isPermissionDenied(permission)) {
processPermission(activity, PERMISSIONS_READ_STORAGE[0], PERMISSIONS_READ_STORAGE, READ_EXTERNAL_PERMISSION_REQUEST_CODE);
}
}
public static void verrifyWriteStoragePermissions(Activity activity) {
int permission = ActivityCompat.checkSelfPermission(activity, PERMISSIONS_WRITE_STORAGE[0]);
if (isPermissionDenied(permission)) {
processPermission(activity, PERMISSIONS_WRITE_STORAGE[0], PERMISSIONS_WRITE_STORAGE, WRITE_EXTERNAL_PERMISSION_REQUEST_CODE);
}
}
public static void verrifyRecordAudioPermissions(Activity activity) {
int permission = ActivityCompat.checkSelfPermission(activity, PERMISSIONS_AUDIO[0]);
if (isPermissionDenied(permission)) {
processPermission(activity, PERMISSIONS_AUDIO[0], PERMISSIONS_AUDIO, RECORD_AUDIO_PERMISSION_REQUEST_CODE);
}
}
public static void verrifyCameraPermissions(Activity activity) {
int permission = ActivityCompat.checkSelfPermission(activity, PERMISSIONS_CAMERA[0]);
if (isPermissionDenied(permission)) {
processPermission(activity, PERMISSIONS_CAMERA[0], PERMISSIONS_CAMERA, CAMERA_PERMISSION_REQUEST_CODE);
}
}
private static boolean isPermissionDenied(int permission) {
return permission != PackageManager.PERMISSION_GRANTED;
}
private static void processPermission(Activity activity, String permissionManifest, String[] permissions, int requestCode) {
if (ActivityCompat.shouldShowRequestPermissionRationale(activity, permissionManifest)) {
Log.e(TAG, "shouldShowRequestPermissionRationale is invoked " + permissionManifest);
} else {
ActivityCompat.requestPermissions(activity, permissions, requestCode);
Log.e(TAG, "requestPermissions is invoked " + permissionManifest);
}
}
/**
* SAMPLER
* Request the permissions you need
If your app doesn't already have the permission it needs, the app must call one of the requestPermissions() methods to request the appropriate permissions.
Your app passes the permissions it wants, and also an integer request code that you specify to identify this permission request.
This method functions asynchronously: it returns right away, and after the user responds to the dialog box,
the system calls the app's callback method with the results, passing the same request code that the app passed to requestPermissions().
* @param activity - you mContext
*/
public static void verifyShowRequestPrompt(Activity activity) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(activity,
Manifest.permission.CAMERA)) {
// Show an explanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
Log.e(TAG, "shouldShowRequestPermissionRationale is invoked");
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(activity,
new String[]{Manifest.permission.CAMERA},
WRITE_EXTERNAL_PERMISSION_REQUEST_CODE);
// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
// app-defined int constant. The callback method gets the
// result of the request.
}
}
/**
* ABOVE CODES is not used
* @param activity
*/
public static void initPermissions(final Activity activity) {
// The request code used in ActivityCompat.requestPermissions()
// and returned in the Activity's onRequestPermissionsResult()
// int PERMISSION_ALL = 1;
final String[] PERMISSIONS = {
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.RECORD_AUDIO,
Manifest.permission.CAMERA};
if(!hasPermissions(activity, PERMISSIONS)) {
showMessageOKCancel(activity, "These permissions are mandatory for the application. Please allow access.",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (!hasPermissions(activity, PERMISSIONS)) {
ActivityCompat.requestPermissions(activity, PERMISSIONS, REQUEST_EXTERNAL_STORAGE);
}
}
});
}
setGAlleryPermissionIntent();
}
public static void initPermissions(final Context context) {
// The request code used in ActivityCompat.requestPermissions()
// and returned in the Activity's onRequestPermissionsResult()
// int PERMISSION_ALL = 1;
final String[] PERMISSIONS = {
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.RECORD_AUDIO,
Manifest.permission.CAMERA};
if(!hasPermissions(context, PERMISSIONS)) {
showMessageOKCancel(context, "These permissions are mandatory for the application. Please allow access.",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (!hasPermissions(context, PERMISSIONS)) {
ActivityCompat.requestPermissions((Activity) context, PERMISSIONS, REQUEST_EXTERNAL_STORAGE);
}
}
});
}
setGAlleryPermissionIntent();
}
public static boolean hasPermissions(final Context context, final String... permissions) {
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null && permissions != null) {
for (String permission : permissions) {
if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
}
return true;
}
private static void showMessageOKCancel(Context context, String message, DialogInterface.OnClickListener okListener) {
new AlertDialog.Builder(context)
.setMessage(message)
.setPositiveButton("OK", okListener)
.setNegativeButton("Cancel", null)
.create()
.show();
}
public static void setGAlleryPermissionIntent() {
if(Build.VERSION.SDK_INT>=24){
try{
Method m = StrictMode.class.getMethod("disableDeathOnFileUriExposure");
m.invoke(null);
}catch(Exception e){
e.printStackTrace();
}
}
}
}
只需在您的 activity 上调用它即可。
void onCreate(...) {
PermissionUtil.verrifyReadStoragePermissions(this);
...
}