tipu
tipu

Reputation: 9604

unable to start service from activity using startService

all tutorials tell me to do this:

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

but there is no constructor that takes Activity, Class and so i get syntax errors

http://developer.android.com/reference/android/content/Intent.html

I am attempting to spawn a service this way,

startService(new Intent(getApplicationContext(), MyService.class));

but my service never executes.

public class MyService extends IntentService {                                                                                                                                                                     

    public MyService() {                                                                                                                                                                                           
        super("MyService");                                                                                                                                                                                        
    }                                                                                                                                                                                                                  

    @Override                                                                                                                                                                                                          
    protected void onHandleIntent(Intent intent) {                                                                                                                                                                     
        //do stuff                                                                                                                                                                                                     
    }                                                                                                                                                                                                                  

}  

how can i spawn a service?

ANSWER: my android manifest didnt specifcy package

Upvotes: 0

Views: 365

Answers (2)

MByD
MByD

Reputation: 137272

Actual problem (as found in comments) added here :

The service was in a different package, so in the manifest it has to be fully qualified, e.g. <service android:name="actual.package.of.MyService"/>

Old Answer:

  1. If you try to launch the service in an inner class, you should not write:

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

    But:

    startService(new Intent(ActivityName.this, MyService.class));
    
  2. Make sure your service appears in the manifest.

  3. Add a debug print inside the onHandleIntent to see whether it starts or not.

Upvotes: 2

Pratik
Pratik

Reputation: 346

Services are not something visible

Possibly you have not declared the service in manifest file.

I dont think there is any problem with the code. Override onstartCommand and oncreate method and just put a log statement and check logcat

Upvotes: 0

Related Questions