81

I have a service class. I have exported this class to jar and I have embed the jar in my client app.

When needed, I call the service class. When I try to do this, I get the following error:

Unable to start service Intent {comp={com.sample.service/com.sample.service.serviceClass}} : not found

I have other class apart from the service class, which I am able to access (create object of that class) which are inside the same jar.

I feel I have missed out some thing in my configuration or manifest or so.

Please help me identifying the same. My code is below:

public void onCreate(Bundle savedInstanceState) {    
      super.onCreate(savedInstanceState);  
      Intent intent = new Intent () ;  
      intent.setClassName("com.sample.service" ,"com.sample.service.serviceClass") ;  
      this.startService(intent) ; // when I call this line I get the message...  
      // binding other process continue  here   
}

Client manifest.xml

<service android:name="com.sample.service.serviceClass"  
            android:exported="true" android:label="@string/app_name" 
            android:process=":remote">
   <intent-filter><action android:name="com.sample.service.serviceClass"></action>
   </intent-filter>
</service>
starball
  • 20,030
  • 7
  • 43
  • 238
Vinay
  • 2,395
  • 3
  • 30
  • 35
  • I tried to fix your code but in the source there are issues I felt I don't understand enough to attempt it. Basically you have an extra space in < service ...>, it should be , but if you look at the source, you have a '/' before the two import statements and I am not certain if they are supposed to be there. I think the spaces after the '<' will be the cause of your problem. – James Black Aug 09 '10 at 11:15
  • Thanks james for the reply. Those spaces are given intentionally only in to display in the site. Inside the code, there are not spaces inside the tags. Even the "\" is only for correct display on the site.. Basically, I do not get any compilation errors in eclipse. Only on runtime, I get the message in LogCat – Vinay Aug 09 '10 at 11:55

6 Answers6

72

For anyone else coming across this thread I had this issue and was pulling my hair out. I had the service declaration OUTSIDE of the '< application>' end tag DUH!

RIGHT:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  ...>
...
<application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity ...>
        ...
    </activity>    

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

    <receiver android:name=".Receiver">
        <intent-filter>
            ...
        </intent-filter>
    </receiver>        
</application>

<uses-permission android:name="..." />

WRONG but still compiles without errors:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  ...>
...
<application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity ...>
        ...
    </activity>

</application>

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

    <receiver android:name=".Receiver">
        <intent-filter>
            ...
        </intent-filter>
    </receiver>        

<uses-permission android:name="..." />

Blundell
  • 75,855
  • 30
  • 208
  • 233
49

First, you do not need android:process=":remote", so please remove it, since all it will do is take up extra RAM for no benefit.

Second, since the <service> element contains an action string, use it:

public void onCreate(Bundle savedInstanceState) {    
      super.onCreate(savedInstanceState);  
      Intent intent=new Intent("com.sample.service.serviceClass");  
      this.startService(intent);
}
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Dear Mr. Mark, I have added the remote because I need to access the same content from the different apps. I include this after read on the web that it is better to implement a remote service. I modified as per your suggestion and it is work fine now. Thanks once again. – Vinay Aug 09 '10 at 13:10
  • 2
    @Vinay: "I need to access the same content from the different apps. " You still do not need `android:process=":remote"`, so please remove it, since all it will do is take up extra RAM for no benefit. A remote service is one that offers an API via AIDL and has nothing to do with the `android:process` attribute. Here is a sample remote service: http://github.com/commonsguy/cw-advandroid/tree/master/AdvServices/RemoteService/ and here is a corresponding client of that service: http://github.com/commonsguy/cw-advandroid/tree/master/AdvServices/RemoteClient/ – CommonsWare Aug 09 '10 at 13:14
  • it seems this doesn't work any more on the latest version of Androids, 23 API works, 30 API doesn't – user924 May 22 '21 at 10:23
  • @user924: Based on [this question that you just asked](https://stackoverflow.com/questions/67648647/android-11-starting-a-service-of-another-app), it appears that you are trying to start a service in another app. That does not seem to be the scenario from this question -- AFAICT, it is for a single app. Plus, this answer is nearly 13 years old, and Android has certainly changed since then. – CommonsWare May 22 '21 at 10:57
32

1) check if service declaration in manifest is nested in application tag

<application>
    <service android:name="" />
</application>

2) check if your service.java is in the same package or diff package as the activity

<application>
    <!-- service.java exists in diff package -->
    <service android:name="com.package.helper.service" /> 
</application>
<application>
    <!-- service.java exists in same package -->
    <service android:name=".service" /> 
</application>
NatNgs
  • 874
  • 14
  • 25
Jianhong
  • 899
  • 9
  • 11
  • 1
    with the dot or not? in my case:not,instead I use and it works.see http://stackoverflow.com/questions/2265020/why-doesnt-my-service-work-in-android-i-just-want-to-log-something-ever-5-seco – DiveInto Sep 21 '11 at 08:36
  • with the dot, it should work too because at the top you define your package path – Jianhong Nov 02 '11 at 07:30
5

I hope I can help someone with this info as well: I moved my service class into another package and I fixed the references. The project was perfectly fine, BUT the service class could not be found by the activity.

By watching the log in logcat I noticed the warning about the issue: the activity could not find the service class, but the funny thing was that the package was incorrect, it contained a "/" char. The compiler was looking for

com.something./service.MyService

instead of

com.something.service.MyService

I moved the service class out from the package and back in and everything worked just fine.

sataniccrow
  • 372
  • 2
  • 7
  • First I thought that / was a error but but if you keep looking at logcat entries you will see it many times and all those apps are working perfectly. – Axxiss Jul 13 '12 at 11:50
  • 1
    well, i cant deny your experience but mine was a little different and by moving in and out again the class from the package, eclipse fixed its own mistake ;) and I got no runtime exception. – sataniccrow Jul 17 '12 at 14:41
  • I think i have the same problem, but i couldn't get it fixed by moving the class file ... – seb Sep 19 '12 at 00:27
  • This was my particular problem. Very hard to figure out but the / in the service name was a clue. If your service suddenly stops working or doesn't work in a fresh checkout, this could very well be the cause (and solution). – Hayes Haugen Jul 10 '13 at 19:14
  • I had the same issue, cleaning the project fixed it. Not sure if it works for everyone but it did for me. I didn't need to take the file out and in the package. – Armando Jan 13 '14 at 17:53
0

I've found the same problem. I lost almost a day trying to start a service from OnClickListener method - outside the onCreate and after 1 day, I still failed!!!! Very frustrating! I was looking at the sample example RemoteServiceController. Theirs works, but my implementation does not work!

The only way that was working for me, was from inside onCreate method. None of the other variants worked and believe me I've tried them all.

Conclusion:

  • If you put your service class in different package than the mainActivity, I'll get all kind of errors
  • Also the one "/" couldn't find path to the service, tried starting with Intent(package,className) and nothing , also other type of Intent starting

  • I moved the service class in the same package of the activity Final form that works

  • Hopefully this helps someone by defining the listerners onClick inside the onCreate method like this:

    public void onCreate() {
    //some code......
        Button btnStartSrv  = (Button)findViewById(R.id.btnStartService);
        Button btnStopSrv  = (Button)findViewById(R.id.btnStopService);
    
        btnStartSrv.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                startService(new Intent("RM_SRV_AIDL"));
            }
        });
    
        btnStopSrv.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                stopService(new Intent("RM_SRV_AIDL"));
            }
        });
    
    } // end onCreate
    

Also very important for the Manifest file, be sure that service is child of application:

<application ... >
    <activity ... >
     ...
    </activity>
    <service
        android:name="com.mainActivity.MyRemoteGPSService"
        android:label="GPSService"
        android:process=":remote">

        <intent-filter>
             <action android:name="RM_SRV_AIDL" />
        </intent-filter>
    </service>
</application>
Jay Atkinson
  • 3,279
  • 2
  • 27
  • 41
marisxanis
  • 109
  • 8
0

In my case the 1 MB maximum cap for data transport by Intent. I'll just use Cache or Storage.

ranbuch
  • 1,584
  • 16
  • 14