Android: unable to start service intent: not found

2019-01-25 04:51发布

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>

8条回答
Viruses.
2楼-- · 2019-01-25 05:48

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

查看更多
成全新的幸福
3楼-- · 2019-01-25 05:48

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>
查看更多
登录 后发表回答