2

I am trying to start IntentService from BroadcastReceiver like ( in onReceive(Context context, Intent intent) function):

            Intent i = new Intent(context, RegService.class);
            i.putExtra("user_id", userId);
            i.putExtra("device_id", "bla");
            context.startService(i);

and RegService like

public class RegService extends IntentService {

    public RegService(String name) {
        super(name);
    }

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



    @Override
    protected void onHandleIntent(Intent intent) {
        // TODO Auto-generated method stub
        String c2dm_registration_id = intent
                .getStringExtra("c2dm_registration_id");
        String token = intent.getStringExtra("token");
        String userId = intent.getStringExtra("user_id");
        String device_type = "android";
        try {

            JSONObject object = RestClient.sendC2DMRegistrationId(
                    c2dm_registration_id, device_type,
                    token, userId);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

but I cannot enter in onHandle in RegService ( I passed through first code context.startService(i); but I cannot enter in onHandle, no message ). Did anybody have same experience and waht is a solution ? I have in manifest

<service
        android:enabled="true"
        android:name=".c2dm.RegService" />
Damir
  • 54,277
  • 94
  • 246
  • 365
  • I dont understand your question to well. "like ( in onReceive(Context context, Intent intent) function):" where is this "onReceive" method your referring too? Also the last paragraph, what does "i cannot enter in onHandle in RegService" mean? – owen gerig Nov 23 '11 at 16:30
  • also posting your manifest.xml might help a little, since i believe your service should be defined in there as well. – owen gerig Nov 23 '11 at 16:32
  • @owengerig inside BroadcastReceiver class inside method onReceive I am executing code with context.startService. I cannot enter means that program never reaches that point or function. But from code above you can see that you can expect to enter in that function. – Damir Nov 23 '11 at 16:34
  • dont know a specific answer but i belive the name needs to match what the intent is calling. like this http://stackoverflow.com/questions/3439356/unable-to-start-service-intent – owen gerig Nov 23 '11 at 16:44
  • I take it RegService is in the package: application_package.c2dm ? – FunkTheMonk Nov 23 '11 at 16:48
  • i have copy pasted above code.Its working fine for me. – Meher Nov 07 '12 at 11:10
  • Hello Damir, Have you managed to solve this issue? – Lizzy Apr 03 '15 at 13:36

2 Answers2

3

When my IntentService was declared with just its class name in the manifest,then I was not able to call it from the Broadcast Receiver.I then gave the full name like :

<service  android:name="com.abc.def.services.DummyService" />

This did the trick and I was able to invoke this IntentService from within the Broadcast Receiver.

Basher51
  • 1,319
  • 1
  • 17
  • 28
1

Have you declared the service in the manifest?

If so and it does not work try to set an action on the intent anyway, evenhough you won't use it.

gwvatieri
  • 5,133
  • 1
  • 29
  • 29