当前位置: 首页 > 知识库问答 >
问题:

Android 6.0权限错误

微生良策
2023-03-14

我得到了这个错误:

getDeviceId: Neither user 10111 nor current process has android.permission.READ_PHONE_STATE.
   at android.os.Parcel.readException(Parcel.java:1599)
   at android.os.Parcel.readException(Parcel.java:1552)

共有1个答案

彭存
2023-03-14

是的,Android M上的权限已经更改,现在在运行时请求权限,而不是在Android M之前的安装时请求。

你可以在这里查文件

这个版本引入了一个新的权限模型,用户现在可以在运行时直接管理app权限。该模型为用户提供了更好的可见性和权限控制,同时简化了应用程序开发人员的安装和自动更新过程。用户可以单独授予或撤销已安装应用程序的权限。

有关在应用程序中支持新权限模型的详细信息,请参见使用系统权限。有关如何评估对应用程序的影响的提示,请参阅权限最佳实践。

要检查权限,您必须像下面这样进行检查,从github中摘取

public class MainActivity extends SampleActivityBase
        implements ActivityCompat.OnRequestPermissionsResultCallback {

    public static final String TAG = "MainActivity";

    /**
     * Id to identify a camera permission request.
     */
    private static final int REQUEST_CAMERA = 0;

    // Whether the Log Fragment is currently shown.
    private boolean mLogShown;

    private View mLayout;

    /**
     * Called when the 'show camera' button is clicked.
     * Callback is defined in resource layout definition.
     */
    public void showCamera(View view) {
        // Check if the Camera permission is already available.
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
                != PackageManager.PERMISSION_GRANTED) {
            // Camera permission has not been granted.

            requestCameraPermission();

        } else {

            // Camera permissions is already available, show the camera preview.
            showCameraPreview();
        }
    }

    /**
     * Requests the Camera permission.
     * If the permission has been denied previously, a SnackBar will prompt the user to grant the
     * permission, otherwise it is requested directly.
     */
    private void requestCameraPermission() {
        if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                Manifest.permission.CAMERA)) {
            // Provide an additional rationale to the user if the permission was not granted
            // and the user would benefit from additional context for the use of the permission.
            // For example if the user has previously denied the permission.
            Snackbar.make(mLayout, R.string.permission_camera_rationale,
                    Snackbar.LENGTH_INDEFINITE)
                    .setAction(R.string.ok, new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            ActivityCompat.requestPermissions(MainActivity.this,
                                    new String[]{Manifest.permission.CAMERA},
                                    REQUEST_CAMERA);
                        }
                    })
                    .show();
        } else {

            // Camera permission has not been granted yet. Request it directly.
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA},
                    REQUEST_CAMERA);
        }
    }

    /**
     * Display the {@link CameraPreviewFragment} in the content area if the required Camera
     * permission has been granted.
     */
    private void showCameraPreview() {
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.sample_content_fragment, CameraPreviewFragment.newInstance())
                .addToBackStack("contacts")
                .commit();
    }

    /**
     * Callback received when a permissions request has been completed.
     */
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
            @NonNull int[] grantResults) {

        if (requestCode == REQUEST_CAMERA) {

            // Received permission result for camera permission.est.");
            // Check if the only required permission has been granted
            if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // Camera permission has been granted, preview can be displayed
                Snackbar.make(mLayout, R.string.permision_available_camera,
                        Snackbar.LENGTH_SHORT).show();
            } else {
                Snackbar.make(mLayout, R.string.permissions_not_granted,
                        Snackbar.LENGTH_SHORT).show();

            }
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mLayout = findViewById(R.id.sample_main_layout);

        if (savedInstanceState == null) {
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            RuntimePermissionsFragment fragment = new RuntimePermissionsFragment();
            transaction.replace(R.id.sample_content_fragment, fragment);
            transaction.commit();
        }
    }
}
 类似资料:
  • 本文向大家介绍详解Android6.0运行时权限管理,包括了详解Android6.0运行时权限管理的使用技巧和注意事项,需要的朋友参考一下 自从Android6.0发布以来,在权限上做出了很大的变动,不再是之前的只要在manifest设置就可以任意获取权限,而是更加的注重用户的隐私和体验,不会再强迫用户因拒绝不该拥有的权限而导致的无法安装的事情,也不会再不征求用户授权的情况下,就可以任意的访问用户

  • 本文向大家介绍android6.0运行时权限完美封装方法,包括了android6.0运行时权限完美封装方法的使用技巧和注意事项,需要的朋友参考一下 前几天看了郭大神的运行时权限的专讲,深受启发,由于现在基于目前项目中的运行时权限封装的还不是那么完美,趁着郭神建议的还是历历在目。于是把它完整的敲了下来。并在此基础上添加上自己的一些见解,封装成一个完整的demo,希望与大家进行交流与。 在这里我进行了

  • 本文向大家介绍android6.0权限动态申请框架permissiondispatcher的方法,包括了android6.0权限动态申请框架permissiondispatcher的方法的使用技巧和注意事项,需要的朋友参考一下 1,添加依赖 在project的build.gradle文件中添加 在module的build.gradle中添加 2,添加注解 注释说明 RuntimePermissio

  • 请不要标记重复,只需更正我的许可,谢谢

  • 本文向大家介绍Android6.0动态申请权限所遇到的问题小结,包括了Android6.0动态申请权限所遇到的问题小结的使用技巧和注意事项,需要的朋友参考一下 白天在做SDK23版本的适配,遇到了不少坑,现在抽空记下来,以此为戒。 首先要知道哪些坑,就得先了解一些定义和基本使用方式。 那么先介绍一下动态申请的权限分组情况。 下面的权限组是由谷歌官方定义的,目的是在申请权限时,只要用户允许同一权限组