Android: unable to start service intent: not found

2019-01-25 04:48发布

问题:

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 came so without .class and with /.Client depending on other settings)

12-02 16:40:15.359: W/ActivityManager(74): Unable to start service Intent { act=com.android.fh.EnOceanApp.Client.class }: not found

In the manifest, this is included in application, out of activities (tried it also in activities and with ".Client"

The code in onCreate()

    startService(new Intent(this, Client.class)); 

or

 startService(new Intent(this.getApplicationContext(), Client.class));

or

Intent intent=new Intent("com.android.fh.EnOceanApp.Client.class");
    this.startService(intent);

or

  Intent intent=new Intent("com.android.fh.EnOceanApp.Client");
    this.startService(intent);

And now, I dont have an Idea anymore.... com.android.fh.EnOceanApp is the package, Client.java the service-class in this package

and the manifest I forgot:

  <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".EnOceanAppActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>      
        </activity>

      <activity android:name=".ListView" 
          android:label="List View">
      </activity> 
      <activity android:name=".GraphView" 
          android:label="Graph View">
      </activity>
      <service 
            android:name=".Client"></service> //with and without ., of course without this comment
    </application>

回答1:

Thanks to user njzk2 for letting me notice what was happening.

I've had the same problem. It seem that Android OS can't find the service class that you've requested if you haven't registered before in the manifest file of your proyect.

Remember that a service is like an activity but without graphic interface. It means that the services needs to be registered before you can use them

This is how you register the service in your Android project:

<application>
    <!-- your code -->
    <activity>
        <!-- your code  -->
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <service android:name="com.your.own.service.class"></service>
</application>

Just Remember that YourService class needs to extend from Service, if not your class won't be a service.

public class YourService extends Service{}


回答2:

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.



回答3:

So.. just to eventually help others or not:

I made a new project, copied the sources and tried to run it: the service was found now. What was the difference, or in other words: what do I think, might give problems: the long package name or the beginning with com.android... In the new project I just chose com.enocean



回答4:

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



回答5:

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.



回答6:

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>


回答7:

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.



回答8:

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