"Unable to start service Intent" error when starting service from an Activity in Android

20,348

I kept digging around and, as I figured, I was making an obvious rookie error. In AndroidManifest.xml, I had the < service> declaration after < application> instead of nested inside it.

Share:
20,348
capitalf
Author by

capitalf

Updated on July 09, 2022

Comments

  • capitalf
    capitalf almost 2 years

    I see the following error in DDMS when trying to use a CheckBox on my MyActivity" activity to start a service called "MyService":

    W/ActivityManager(   73): Unable to start service Intent { cmp=com.example.android.myprogram/.MyService }: not found
    

    I used the tutorial http://developer.android.com/resources/tutorials/views/hello-formstuff.html and added the provided code to the end of my onCreate() method. I have the classes specified separately in MyActivity.java and MyService.java.

    package com.example.android.myprogram;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.CheckBox;
    
    
    public class MyActivity extends Activity {
        private static final String TAG = "MyActivity";
    
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            final CheckBox checkbox = (CheckBox) findViewById(R.id.checkbox);
            checkbox.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    // Perform action on clicks, depending on whether it's now checked
                    if (((CheckBox) v).isChecked()) {
                        // TODO: Add code to START the service
                        Log.d(TAG, "startService from checkbox");     
                        startService(new Intent(MyActivity.this, MyService.class));
                    } else {
                        // TODO: Add code to STOP the service
                        Log.d(TAG, "stopService from checkbox");     
                        stopService(new Intent(MyActivity.this, MyService.class));
                    }
                }
            });
        }
    }
    

    My manifest file does have the following in which I've also tried the full namespace, short name, using an intent-filter per another search, etc. I'm not saying what is there is correct. I just left it at a stopping point.

    <service android:name=".MyService">
       <intent-filter><action android:name="com.example.android.myprogram.MyService"></action>
       </intent-filter>
    </service>
    

    And lastly, my service which I've decided to break down to it's bare minimum:

    package com.example.android.myprogram;
    
    
    import android.app.Service;
    import android.content.Intent;
    import android.os.IBinder;
    import android.util.Log;
    
    public class MyService extends Service {
        private static final String TAG = "MyService";
    
        @Override
        public IBinder onBind(Intent intent) {
            return null;
        }
    
        @Override
        public void onCreate() {
            Log.d(TAG, "onCreate");
            //code to execute when the service is first created
        }
    
        @Override
        public void onDestroy() {
            Log.d(TAG, "onDestroy");
            //code to execute when the service is shutting down
        }
    
        @Override
        public void onStart(Intent intent, int startid) {
            Log.d(TAG, "onStart");
            //code to execute when the service is starting up
        }
    }
    

    I'm very, very, very new to Java/Android programming and programming in general (but learning) so I'm sure this is user error and probably common sense to everyone else. Any suggestions would be great.

  • capitalf
    capitalf over 13 years
    Thanks for the reply. I actually already removed that line since originally posting after remembering that's only for 3rd party applications to use my service. Unfortunately, I still receive the error.
  • Dam
    Dam almost 8 years
    Obviously you saved me so much time today. Thank you.