There is a service and application implemented by different projects. In Android 4, everything works fine. When you receive an SMS or press a button in the application, it is executed:

public void onClickStart(View v) { Intent intent = new Intent("com.example.kir.myapplication.service") .putExtra(KEY, STATUS_CODE_START); startService(intent); Log.d(LOG_TAG,"onClickStart"); } 

with startService(intent); starts a service that performs the necessary operations. But when launched on Android 5.1, the application crashes with the following error.

  05-23 12:25:39.167 10539-10539/com.example.kir.myapplication E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.kir.myapplication, PID: 10539 java.lang.IllegalStateException: Could not execute method of the activity at android.view.View$1.onClick(View.java:4074) at android.view.View.performClick(View.java:4848) at android.view.View$PerformClick.run(View.java:20262) at android.os.Handler.handleCallback(Handler.java:815) at android.os.Handler.dispatchMessage(Handler.java:104) at android.os.Looper.loop(Looper.java:194) at android.app.ActivityThread.main(ActivityThread.java:5637) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754) Caused by: java.lang.reflect.InvocationTargetException at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at android.view.View$1.onClick(View.java:4069) at android.view.View.performClick(View.java:4848) at android.view.View$PerformClick.run(View.java:20262) at android.os.Handler.handleCallback(Handler.java:815) at android.os.Handler.dispatchMessage(Handler.java:104) at android.os.Looper.loop(Looper.java:194) at android.app.ActivityThread.main(ActivityThread.java:5637) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754) Caused by: java.lang.IllegalArgumentException: Service Intent must be explicit: Intent { act=com.example.kir.myapplication.service (has extras) } at android.app.ContextImpl.validateServiceIntent(ContextImpl.java:1801) at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1830) at android.app.ContextImpl.startService(ContextImpl.java:1814) at android.content.ContextWrapper.startService(ContextWrapper.java:516) at com.example.kir.myapplication.MainActivity.onClickStart(MainActivity.java:63) 

    1 answer 1

    Intent to start the service should be created not as new Intent("com.example.kir.myapplication.service") , as well as new Intent(context, MyService.class) .

    You are hinted at in the error message "Service Intent must be explicit". That is, the intent should explicitly indicate which service it starts, and you create an intent with the action "com.example.kir.myapplication.service", which anyone can theoretically handle.

    If you need to start a service from another application and accordingly you cannot specify its class, you can do this:

     new Intent().setComponent(new ComponentName(servicePackage, serviceClass)); 

    where servicePackage is the name of the application package with the service, and serviceClass is respectively the full name of the service class.

    • I was looking for this opportunity to correct the problem. Tell me, please, how can I explicitly indicate the class of service if it is in another project - x555xx
    • @ x555xx added a response with this option - xkor
    • ** Many thanks, very helpful! ** on 4.4 and 5.1 everything works. The only thing I’ll add, by default, Android Studio adds a service with the name of a class without a package to the manifest: `<service android: name =". MyService "` experimented with the options, it only worked for me if I specify `<service android: name =" com .example.kir.s001.MyService "` and in the intent for the value of the service class also specify the class with the package `New Intent (). setComponent (new ComponentName (" com.example.kir.s001 "," com.example.kir .s001.MyService "));` - x555xx
    • <service android:name=".MyService" absolutely equivalent to <service android:name="com.example.kir.s001.MyService" if the package name is "com.example.kir.s001". Actually the first option is just a reduction of the second. - xkor