Caused by: java.lang.IllegalArgumentException: Service Intent must be explicit: Intent { act=com.aid

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/Maiduoudo/article/details/97933647

Solve Service Intent must be explicit crash

Caused by: java.lang.IllegalArgumentException:
Service Intent must be explicit: Intent { act=com.aidl.server.myserver }



Today, when writing a function to communicate between the app, there have been some small problems, client service through binding aidl end of service, resulting in a crash while client-side application is started, the initial code written like this:

@Override
protected void onCreate(Bundle savedInstanceState) {
    ...
    Intent intent = new Intent("com.aidl.server.myserver");
    bindService(intent, conn, Context.BIND_AUTO_CREATE);
}



Error log as follows:

Service Intent must be explicit: Intent { act=com.aidl.server.myserver }



With a look at the source code and found the following judgments ContextImpl in:

@Override
public boolean bindService(Intent service, ServiceConnection conn,
            int flags) {
    warnIfCallingFromSystemProcess();
    return bindServiceCommon(service, conn, flags, mMainThread.getHandler(),
            Process.myUserHandle());
}
private boolean bindServiceCommon(Intent service, ServiceConnection conn, int flags, Handler
            handler, UserHandle user) {
    ...
    validateServiceIntent(service);
    ...
}

private void validateServiceIntent(Intent service) {
    if (service.getComponent() == null && service.getPackage() == null) {
        if (getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP) {
            IllegalArgumentException ex = new IllegalArgumentException(
                        "Service Intent must be explicit: " + service);
            throw ex;
        } else {
            Log.w(TAG, "Implicit intents with startService are not safe: " + service
                    + " " + Debug.getCallers(2, 3));
        }
    }
}



Thus, the judgment of increase in Android5.0 intent, since the intent is obtained by setting action, there is no Component object instance, there is no package name, and therefore error. The reason is found, adds a step to set the package name, and the need is App package name, package name rather than the Service class where the package can be resolved smoothly, as follows:

I

        Intent intent = new Intent("com.aidl.server.myserver");
        intent.setPackage("com.aidl.server.myserver");
        bindService(intent, conn, Context.BIND_AUTO_CREATE);


 

Guess you like

Origin blog.csdn.net/Maiduoudo/article/details/97933647