其他分享
首页 > 其他分享> > requestPermissions NameNotFoundException android M

requestPermissions NameNotFoundException android M

作者:互联网

我有以下代码:

public static void requestPermissions(final Activity activity){

    System.out.println("Permission 0 : " + activity.getPackageName());

    int permission = activity.getPackageManager().checkPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, activity.getPackageName());

    if(permission != PackageManager.PERMISSION_GRANTED){
        PackageManager pm = activity.getPackageManager();

        try {
            PackageInfo pi = pm.getPackageInfo(activity.getPackageName(), PackageManager.GET_ACTIVITIES);

            System.out.println("Permission package info : " + pi);
        } catch (NameNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        System.out.println("Permission 1 : " + activity);
        ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0xFFEEDDCC);
    }
}

无论我如何尝试,我总是会得到以下结果:

10-19 17:35:31.642: I/System.out(27952): Permission 0 : com.example.testappandroid
10-19 17:35:31.643: I/System.out(27952): Permission package info : PackageInfo{7b81ba7 com.example.testappandroid}
10-19 17:35:31.643: I/System.out(27952): Permission 1 : com.example.testappandroid.MainActivity@c40298e
10-19 17:35:31.644: I/ActivityManager(862): START u0 {act=android.content.pm.action.REQUEST_PERMISSIONS pkg=com.google.android.packageinstaller cmp=com.google.android.packageinstaller/com.android.packageinstaller.permission.ui.GrantPermissionsActivity (has extras)} from uid 10109 on display 0
10-19 17:35:31.668: I/GrantPermissionsActivity(4527): No package: null
10-19 17:35:31.668: I/GrantPermissionsActivity(4527): android.content.pm.PackageManager$NameNotFoundException
10-19 17:35:31.668: I/GrantPermissionsActivity(4527):   at android.app.ApplicationPackageManager.getPackageInfo(ApplicationPackageManager.java:137)
10-19 17:35:31.668: I/GrantPermissionsActivity(4527):   at com.android.packageinstaller.permission.ui.GrantPermissionsActivity.getCallingPackageInfo(GrantPermissionsActivity.java:312)
10-19 17:35:31.668: I/GrantPermissionsActivity(4527):   at com.android.packageinstaller.permission.ui.GrantPermissionsActivity.onCreate(GrantPermissionsActivity.java:94)

这可能是一个非常简单的问题.正如从LogCat的日志中看到的那样,传递给函数requestPermissions的活动是主要活动. PackageManager.getPackageInfo()的用法似乎也是正确的,因为如果我隔离它,它将按预期工作.

此外,如预期的那样,在清单中定义了Manifest.permission.WRITE_EXTERNAL_STORAGE.

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

编辑

一个重要的事实是,我的静态函数requestPermissions(Activity)是在外部库中定义的,该外部库称为utils.jar.因此,如果我在MainActivity.java中编写以下代码:

protected void onStart(){
    super.onStart();
    final Activity act = this;
    Utils.requestPermissions(act); // in utils.jar
}

它会失败.如果我这样写:

protected void onStart(){
    super.onStart();
    final Activity act = this;
    ActivityCompat.requestPermissions(act, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
}

它将按预期要求权限.

编辑结束

有没有人曾经经历过这种情况并且可以有所启发?
先感谢您.-

解决方法:

只需在肯定的事情上更改requestCode的值即可.

ActivityCompat.requestPermissions在内部某处调用startActivityForResult并使用您的requestCode.如果为负,将引发与您相同的异常:

I/GrantPermissionsActivity: No package: null
                            android.content.pm.PackageManager$NameNotFoundException

只需更改requestCode,它将像在应用程序中一样在库中工作.
例如:

ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 101);

标签:android-permissions,android-6-0-marshmallow,android-activity,android
来源: https://codeday.me/bug/20191119/2037125.html