从广播接收器启动服务

时间:2014-09-12 10:37:16

标签: java android

如果我从Service启动Activity,它就会正常运行(点击按钮)。但是,如果我从BrodcastReceiver启动它,它就不起作用。我究竟做错了什么? 这是我的ServiceBroadcastReceiver

代码

http://pastebin.com/hDDrhg0B

http://pastebin.com/ey7bnJSw

这是我的清单文件 http://pastebin.com/fUcM6Qse

1 个答案:

答案 0 :(得分:1)

我是这样做的,它有效:

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);
        }
    }
}