Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix Calling a method in the system process without a qualified user this error in the system android application?

In the settings application I'm trying to receive message about changing network status. It looks like this:

 <receiver android:name="com.android.settings.NetworkStateReceiver">
        <intent-filter>
            <action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
        </intent-filter>
 </receiver>



public class NetworkStateReceiver extends BroadcastReceiver
{
  public static final String TAG = "NetworkStateReceiver";
  public static final String NETWORK_CHANGED_ACTION = "com.android.settings.NetworkStateReceiver.NETWORK_CHANGED";

  @Override
  public void onReceive( Context context, Intent intent )
  {
      Log.d(TAG, "Network state changed");
      Intent i = new Intent();
      i.setAction(NETWORK_CHANGED_ACTION);
      context.sendBroadcast(i);
  }
}

From this receiver I'm trying to send message to DeviceInfoFragment, there is receiver, which should catch this broadcast:

 private BroadcastReceiver mNetwrokStateReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {

        Log.d("NetworkStateReceiver_DEVICE_INFO", "network state changed");
        if (isNetworkConnected()) {
            Log.d("NetworkStateReceiver_DEVICE_INFO", "network connected");
            mHelpPreference.setEnabled(true);
        } else {
            mHelpPreference.setEnabled(false);
            Log.d("NetworkStateReceiver_DEVICE_INFO", "network disconnected");
        }
    }
};

I register it in onResume and unregister in onPause.

But I got error in NetworkStateReceiver in this line:

context.sendBroadcast(i);

Calling a method in the system process without a qualified user

Is there is any way to fix it?

like image 485
Pein Avatar asked Sep 27 '16 11:09

Pein


2 Answers

It is just a warning.

You can see that by looking at the source code: https://android.googlesource.com/platform/frameworks/base/+/b267554/core/java/android/app/ContextImpl.java

Just search for the method "warnIfCallingFromSystemProcess()"

like image 155
Bamerza Avatar answered Oct 20 '22 12:10

Bamerza


If you are developing market applications you cannot eliminate this warning.

If you are a handset vendor, you can add to your manifest

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

and then can send the broadcast as an actual user.

context.sendBroadcastAsUser(i, new UserHandle(UserHandle.USER_CURRENT));

The application that is launched will get elevated permissions only if it is properly configured to do so.

like image 35
Brent K. Avatar answered Oct 20 '22 12:10

Brent K.