请求权限 READ_SMS 后,包安装程序在 Android M 时崩溃
Package Installer crashes in Android M after requesting permission READ_SMS
我有一个尝试访问 (TelephonyManager) getActivity().getSystemService(Context.TELEPHONY_SERVICE);
的登录片段,这适用于最高 Lollipop 5.1 的设备。当我在 Marshmallow 6.01 中尝试它时,它显示了安全异常。所以我添加了 android 文档中的代码来请求运行时权限。这是代码
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);
}
现在我的应用启动了,很快我的包安装程序就崩溃了。这是日志
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)
我认为当应用程序试图显示 UI 以请求权限时,程序包安装程序崩溃了。
我已经在清单
中添加了 <uses-permission android:name="android.permission.READ_SMS"/>
像这样在清单中声明权限:
<uses-permission android:name="android.permission.READ_SMS" />
注意,没有 maxSdkVersion
属性。 Reference.
您已将 READ_SMS
清单权限限制为 API 22 及以下。
<uses-permission android:name="android.permission.READ_SMS" android:maxSdkVersion="22" />
正如我在评论中提到的,除了运行时请求之外,Marshmallow 及更高版本仍然需要此清单权限。从权限元素中删除 maxSdkVersion
属性。
我有一个尝试访问 (TelephonyManager) getActivity().getSystemService(Context.TELEPHONY_SERVICE);
的登录片段,这适用于最高 Lollipop 5.1 的设备。当我在 Marshmallow 6.01 中尝试它时,它显示了安全异常。所以我添加了 android 文档中的代码来请求运行时权限。这是代码
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);
}
现在我的应用启动了,很快我的包安装程序就崩溃了。这是日志
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)
我认为当应用程序试图显示 UI 以请求权限时,程序包安装程序崩溃了。 我已经在清单
中添加了<uses-permission android:name="android.permission.READ_SMS"/>
像这样在清单中声明权限:
<uses-permission android:name="android.permission.READ_SMS" />
注意,没有 maxSdkVersion
属性。 Reference.
您已将 READ_SMS
清单权限限制为 API 22 及以下。
<uses-permission android:name="android.permission.READ_SMS" android:maxSdkVersion="22" />
正如我在评论中提到的,除了运行时请求之外,Marshmallow 及更高版本仍然需要此清单权限。从权限元素中删除 maxSdkVersion
属性。