Android代码异常Calling a method in the system process without a qualified user

problem causes:

Sometimes it is found that startService or sendBroadCast will generate this exception: a method in the system process is called without a qualified user. After investigation, the reason is that the system application throws this exception when trying to launch a normal application. It may be that after Android 4.2, Android introduced multi-user support, and some special permissions will fail to start services or send broadcasts.

How to modify:

That is, change the previous startup service or send broadcast to the following calling method:

public void startActivityAsUser(Intent intent, UserHandle user); 
public void sendBroadcastAsUser(Intent intent, UserHandle user); 
public ComponentName startServiceAsUser(Intent service, UserHandle user); 
public boolean stopServiceAsUser(Intent service, UserHandle user); 

UserHandle.ALL 
UserHandle.CURRENT 
UserHandle.CURRENT_OR_SELF
UserHandle.OWNER

in:

/** A user handle to indicate all users on the device */  
//设备上所有用户均可接收到广播  
// handle = -1  
UserHandle.ALL  
  
/** A user handle to indicate the current user of the device */  
//设备上当前用户可接收到广播  
// handle = -2  
UserHandle.CURRENT  
  
/** A user handle to indicate that we would like to send to the current 
 *  user, but if this is calling from a user process then we will send it 
 *  to the caller's user instead of failing wiht a security exception */  
//handle = -3  
//设备上当前用户或者该应用所属用户可接收到广播  
UserHandle.CURRENT_OR_SELF  
  
/** A user handle to indicate the primary/owner user of the device */  
// handle = 0  
//设备所有者可接收到广播  
UserHandle.OWNER 

For example:

Intent intent = new Intent();
intent.setAction("com.android.server.light.brightness");
intent.putExtra("brightness", brightness);
//send broadcast

// mContext.sendBroadcast(intent);   --舍弃
// mContext.sendBroadcastAsUser(intent, UserHandle.ALL);

If you can't call it, you might as well take a look at the source code modification:

context.sendBroadcastAsUser(intent, UserHandle.getUserHandleForUid(-1));

Parcel parcel = Parcel.obtain();
parcel.writeInt(-1);
context.sendBroadcastAsUser(null, new UserHandle(parcel));

Guess you like

Origin blog.csdn.net/Jason_Lee155/article/details/125843832