手机睡眠时,BroadcastReceiver需要唤醒锁定吗?

时间:2014-09-08 17:52:29

标签: android android-broadcast android-wake-lock

尝试编写处理传入SMS的广播接收器。我是否需要使用唤醒锁定/部分唤醒锁定才能使此应用程序正常工作,尽管设备由于缺乏前台活动而进入睡眠状态?

1 个答案:

答案 0 :(得分:1)

我倾向于扩展WakefulBroadcastReceiver以简化事情,所以在某种程度上是肯定的。例如:

public class MyBroadcastReceiver extends WakefulBroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        final ComponentName comp = new ComponentName(context.getPackageName(),
                MyIntentService.class.getName());
        // Start the service, keeping the device awake while it is launching.
        startWakefulService(context, (intent.setComponent(comp)));
        setResultCode(Activity.RESULT_OK);
    }
}