我希望你能帮忙。我在任何地方都找不到关于此的任何文档。我在我的 Android 应用 WRITE_EXTERNAL_STORAGE 中请求运行时权限。
在 Android M 下,这可以按预期工作。我点击“允许”,一切都很好。在 Android N 下,单击“允许”会杀死我的应用程序。它不会崩溃,它只是停止。
在 logcat 它显示
Killing 6599:com.myname.myappname/u0a223 (adj 100): permission grant or revoke changed gids
Force removing ActivityRecord{c834991 u0 com.myname.myappname/.MainActivity t1814}: app died, no saved state
Destroying surface Surface(name=Window{bf01619 u0 com.myname.myappname/com.myname.myappname.MainActivity}) called by com.android.server.wm.WindowStateAnimator.destroySurface:-1 com.android.server.wm.WindowStateAnimator.destroySurfaceLocked:-1 com.android.server.wm.WindowState.removeLocked:-1 com.android.server.wm.WindowManagerService.removeWindowInnerLocked:-1 com.android.server.wm.WindowManagerService.removeWindowLocked:-1 com.android.server.wm.WindowManagerService.removeWindowLocked:-1 com.android.server.wm.AppWindowToken.removeAllWindows:-1 com.android.server.wm.AppWindowToken.removeAppFromTaskLocked:-1
查看 Android 源代码,我在 PackageManagerService.java 中找到了“permission grant or revoke changed gids”
private static final String KILL_APP_REASON_GIDS_CHANGED = "permission grant or revoke changed gids";
和
final int result = permissionsState.grantRuntimePermission(bp, userId);
switch (result) {
case PermissionsState.PERMISSION_OPERATION_FAILURE: {
return;
}
case PermissionsState.PERMISSION_OPERATION_SUCCESS_GIDS_CHANGED: {
mHandler.post(new Runnable() {
@Override
public void run() {
killSettingPackagesForUser(sb, userId, KILL_APP_REASON_GIDS_CHANGED);
}
});
我以前可以正常工作的应用程序代码是:
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
接着
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) {
if (requestCode == 1) {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// do stuff
}
谁能向我解释为什么该应用程序被杀死?并且可能如何捕获它以便以编程方式重新启动应用程序?