Android: unable to start service intent: not found?

后端 未结 8 1379
清酒与你
清酒与你 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 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

        
            
                
            
            
                
            
        
    

    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 :)

提交回复
热议问题