首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >android 6系统警报许可!

android 6系统警报许可!
EN

Stack Overflow用户
提问于 2017-11-10 08:15:53
回答 1查看 642关注 0票数 1

我为android 6做了几次运行时预演,但是每次重新启动设备或按一下按钮来服务,就会出现一个对话框,显示系统警报,并在上面读取联系人权限。我的应用程序工作在后台(重新启动后广播调用的服务)--这是第一个权限,我注意到,在它上不再有“永不再问”选项,

,这是我说的第二个系统警报,

现在我怎样才能给这个权限一次,再也不问了,我已经在检查权限了,但是这是另一回事,它只是为了读取联系人权限

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-11-10 08:32:05

这已经出现在Android android.developer.com的文档中了。

Android发出请求的方式取决于系统版本,以及应用程序所针对的系统版本:

如果设备运行的是Android6.0(APILevel23)或更高的__,而应用程序的targetSdkVersion为23或更高的__,则应用程序在运行时向用户请求权限。用户可以随时撤销权限,所以每次访问受权限保护的API时,应用程序都需要检查它是否具有权限。有关在应用程序中请求权限的详细信息,请参阅“使用系统权限培训指南”。

您可能设置了app's targetSdkVersion is 22 or lower

如前所述:

如果设备运行的是Android5.1.1 (API级别22)或更低,或者应用程序的targetSdkVersion为22或更低,系统要求用户在用户安装应用程序时授予权限。如果您向该应用程序的更新版本添加了新权限,系统将要求用户在用户更新该应用程序时授予该权限。一旦用户安装了应用程序,他们唯一可以撤销权限的方法就是卸载应用程序。

您还可以使用我的自定义权限类

代码语言:javascript
运行
复制
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();
            }
        }
    }
}

就当这是你的活动吧。

代码语言:javascript
运行
复制
   void onCreate(...) {
      PermissionUtil.verrifyReadStoragePermissions(this);
      ...
   }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47218330

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档