Permission Denial: broadcast from android asks to run as user -2 but is calling from user 0;

解决办法:

		long ident = Binder.clearCallingIdentity();
		try {
			Intent intent = new Intent("xxx.xxx.xxx");
			// do sometings
			mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
            Binder.restoreCallingIdentity(ident);
        }

原因:
binder跨进程调用,APP调用API执行到系统服务里面时,持有的是APP的 Identity,如果执行的代码需要系统服务权限,需要清除binder的 Identity ,执行完成后再还原。

报错日志:
Permission Denial: broadcast from android asks to run as user -2 but is calling from user 0

system_process W/ActivityManager: Permission Denial: broadcast from android asks to run as user -2 but is calling from user 0; this requires android.permission.INTERACT_ACROSS_USERS_FULL or android.permission.INTERACT_ACROSS_USERS                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
system_process W/System.err: java.lang.SecurityException: Permission Denial: broadcast from android asks to run as user -2 but is calling from user 0; this requires android.permission.INTERACT_ACROSS_USERS_FULL or android.permission.INTERACT_ACROSS_USERS                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
system_process W/System.err:     at com.android.server.am.UserController.handleIncomingUser(UserController.java:1532)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
system_process W/System.err:     at com.android.server.am.ActivityManagerService.broadcastIntentLocked(ActivityManagerService.java:21548)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
system_process W/System.err:     at com.android.server.am.ActivityManagerService.broadcastIntent(ActivityManagerService.java:22263)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
system_process W/System.err:     at android.app.ContextImpl.sendBroadcastAsUser(ContextImpl.java:1186)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
system_process W/System.err:     at android.app.IActivityManager$Stub.onTransact(IActivityManager.java:2264)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          
system_process W/System.err:     at com.android.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:3369)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
posted @ 2022-03-12 22:04  xiaomodecnblogs  阅读(181)  评论(0编辑  收藏  举报