Wake lock required for BroadcastReceiver while phone sleeps?

穿精又带淫゛_ 提交于 2019-12-12 05:03:39

问题


Trying to write a Broadcast Receiver that processes incoming SMSs. Do I need to use a wake lock / partial wake lock, for this application to work, in spite of device going to sleep due to lack of foreground activity ?


回答1:


I tend to extend a WakefulBroadcastReceiver to simplify things, so in a way yes. For example:

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


来源:https://stackoverflow.com/questions/25730227/wake-lock-required-for-broadcastreceiver-while-phone-sleeps

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!