简体   繁体   中英

Broadcast receiver and service for screen off/on

Alright, so i am having some problems trying to get a broadcast receiver and service to work properly with screen off and screen on.

What i am trying to do is start something when the screen goes off or when the screen goes on. I got it to work from an activity for testing, but the activity must be currently running. I need it to start from the background pretty much.

Now, i know that using the intent filters in the manifest does not work for screen_off and on. How would i be able to do this? I guess this would work sort of like a lockscreen...

Screen off --> starts something ( example activity or create a log message as a toast wouldn't work )

Add a receiver:

public class BroadcastReceiverScreenListener extends BroadcastReceiver {

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

    if (Objects.equals(intent.getAction(), Intent.ACTION_SCREEN_OFF)) {

        ** Do your stuff**

    }
}

From the docs: You cannot receive this through components declared in manifests, only by explicitly registering for it with registerReceiver(BroadcastReceiver, IntentFilter) This is a protected intent that can only be sent by the system.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM