Android: unable to start service intent: not found?

后端 未结 8 1374
清酒与你
清酒与你 2020-12-17 14:19

I know, I am not the first onbe with this problem, but I tried so many solutions, I have found and no one works... maybe you could find the error

The error (also cam

相关标签:
8条回答
  • 2020-12-17 14:50

    It is stupid mistake of android

    This will not work

    <service
                android:name=".classname"/>
    

    But this will work, have separate closing tag

    <service
                android:name=".classname"></service>
    
    0 讨论(0)
  • 2020-12-17 14:55

    I made the silly mistake of adding the tag to a separate in the manifest.

    In that case, the current application was unable to find the service defined.

    Hope you skip that mistake :)

    Thanks.

    0 讨论(0)
  • 2020-12-17 15:01

    Despite ALL the answers in this post and many related Unable to start service Intent: not found Unable to start Service Intent , I still struggled and it took some time for me to get this going. My scenario was slightly more complicated since I'm trying to start a service in a DIFFERENT app that the one I'm calling it with. I figured it out and here are ALL the details, along with some bonus code.

    MainActivity of calling intent (or whereever)

    Intent intent=new Intent("com.example.core.MusicService.1234");
    //Or Intent intent=new Intent("com.example.core.MusicService.TOGGLE_PLAYBACK");
    PendingIntent pendingIntent = PendingIntent.getService(this, 99, intent, PendingIntent.FLAG_CANCEL_CURRENT);
    

    Manifest: of Service (Service tag inside Application tag) It's

        <service android:name="com.example.core.MusicService">
            <intent-filter>
                <action android:name="com.example.core.MusicService1234"></action>
            </intent-filter>
            <intent-filter>
                <action android:name="com.example.core.MusicService.TOGGLE_PLAYBACK"></action>
            </intent-filter>
        </service>
    

    MusicService.java

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if(intent != null){
            if(intent.getAction() != null){
                if(intent.getAction().contentEquals("com.example.core.MusicService.TOGGLE_PLAYBACK")){
                    //Do work here
                }
            }
        }
    }
    

    Notes

    • Service does NOT need to be started, this intent will start it
    • "com.example.core.MusicService1234" and "com.example.core.MusicService.TOGGLE_PLAYBACK" can be whatever you want it to be, but obviously needs to match the intent-filter in the service manifest with the calling intent. You can put multiple of these so you can do different actions when your service starts depending on the value from your intent
    • 99 can be whatever you want, but must be unique if you're using notifications
    • I'm not sure it's possible to call a service in a different app (like this) without using the intent-filter - if it is, someone please enlighten us. I tried and it doesn't work.

    Credit to: the cumulative information from all the posts :)

    0 讨论(0)
  • 2020-12-17 15:02

    Well, in my case i had to clean the project. It sometimes happens when you have made a new Java class for the service in your package/project but did not build/clean the project afterwords. In my case, i just had to clean the project to get rid of the error.

    0 讨论(0)
  • 2020-12-17 15:08

    Sometimes you'll need to fully qualify your class name in the manifest, rather than using the shortform (.classname). I've seen that when I used classes from a different package, but perhaps it would help here since the service intent may go outside of the app.

    0 讨论(0)
  • 2020-12-17 15:09

    If anyone sees this and has the same problem that I did, it was because I followed a guide and used context.startService() instead of context.startActivity()

    0 讨论(0)
提交回复
热议问题