Package Installer crashes in Android M after requesting permission READ_SMS

混江龙づ霸主 提交于 2019-12-02 00:04:14

问题


I have a Login Fragment that tries to access (TelephonyManager) getActivity().getSystemService(Context.TELEPHONY_SERVICE); this works well with devices up to Lollipop 5.1. And when I tried it in Marshmallow 6.01 it showed security exception. So I added code from android docs to request permission on runtime. Here is the code

int permissionCheck = ContextCompat.checkSelfPermission(getActivity(),Manifest.permission.READ_SMS);
    if (permissionCheck != PackageManager.PERMISSION_GRANTED) {

        // Should we show an explanation?
        if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(), Manifest.permission.READ_SMS)) {

            // Show an expanation 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.



        } else {

            // No explanation needed, we can request the permission.

            ActivityCompat.requestPermissions(getActivity(),new String[]{Manifest.permission.READ_SMS},REQUEST_SMS);

            // REQUEST_SMS is an
            // app-defined int constant. The callback method gets the
            // result of the request.
        }
    }else {
        tMgr = (TelephonyManager) getActivity().getSystemService(Context.TELEPHONY_SERVICE);

    }

Now my app starts and soon my package installer crashes. Here is the log

03-23 12:12:13.618 8949-8949/com.android.packageinstaller E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.android.packageinstaller, PID: 8949
    Theme: themes:{default=overlay:system, iconPack:system, fontPkg:system, com.android.systemui=overlay:system}
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.packageinstaller/com.android.packageinstaller.permission.ui.GrantPermissionsActivity}: java.lang.NullPointerException: Attempt to get length of null array
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2450)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2520)
        at android.app.ActivityThread.-wrap11(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5466)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
     Caused by: java.lang.NullPointerException: Attempt to get length of null array
        at com.android.packageinstaller.permission.ui.GrantPermissionsActivity.computePermissionGrantState(GrantPermissionsActivity.java:293)
        at com.android.packageinstaller.permission.ui.GrantPermissionsActivity.updateDefaultResults(GrantPermissionsActivity.java:343)
        at com.android.packageinstaller.permission.ui.GrantPermissionsActivity.onCreate(GrantPermissionsActivity.java:100)
        at android.app.Activity.performCreate(Activity.java:6251)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2403)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2520) 
        at android.app.ActivityThread.-wrap11(ActivityThread.java) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:148) 
        at android.app.ActivityThread.main(ActivityThread.java:5466) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

I think the package installer is crashing when the app is trying to display UI for requesting permissions. I have already added <uses-permission android:name="android.permission.READ_SMS"/> in my manifest


回答1:


You've restricted the READ_SMS manifest permission to API 22 and below.

<uses-permission android:name="android.permission.READ_SMS" android:maxSdkVersion="22" />

As I mentioned in the comments, this manifest permission is still needed in Marshmallow and above, in addition to the runtime request. Remove the maxSdkVersion attribute from the permission element.




回答2:


Declare permission in manifest like this:

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

Mind it, there is no maxSdkVersion attribute. Reference.



来源:https://stackoverflow.com/questions/36171595/package-installer-crashes-in-android-m-after-requesting-permission-read-sms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!