kg2152
kg2152

Reputation: 168

Start service from Broadcast receiver

If I start my Service from Activity it runs properly (by clicking button). However it does not work if I start it from BrodcastReceiver. What am I doing wrong? Here is my code of Service, BroadcastReceiver

http://pastebin.com/hDDrhg0B

http://pastebin.com/ey7bnJSw

Here is my manifest file http://pastebin.com/fUcM6Qse

Upvotes: 2

Views: 2547

Answers (1)

Ciprian
Ciprian

Reputation: 2899

Here is how I do it, and it works:

public class BadgesReceiver extends BroadcastReceiver {
    private static final String TAG = "BadgesReceiver";

    public static final String BADGES_UPDATE_ACTION = BuildConfig.PACKAGE_NAME + ".action.UPDATE_BADGES";

    @Override
    public void onReceive(Context context, Intent intent) {
        final String action = intent != null ? intent.getAction() : null;
        Log.d(TAG, "onReceive action: " + action);
        if (!TextUtils.isEmpty(action) && action.equals(BADGES_UPDATE_ACTION)) {
            final Intent i = new Intent(context, MyService.class);
            i.setAction(MyService.UPDATE_BADGES_ACTION);
            context.startService(i);
        }
    }
}

Upvotes: 1

Related Questions