Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
459 views
in Technique[技术] by (71.8m points)

broadcastreceiver - Is it possible to write an Android broadcast receiver that detects when the phone wakes up?

I want to figure out how to detect when the phone wakes up from being in the black screen mode and write a handler for that event. Is that possible? It seems like this would be something a Broadcast Receiver should handle? Or is there a better or more proper way?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you have a Service that it active you can catch these events with

registerReceiver(new BroadcastReceiver() {

  @Override
  public void onReceive(Context context, Intent intent) {
    // do something
  }
}, new IntentFilter(Intent.ACTION_SCREEN_ON));

However this relies on having a perpetually running service which I have recently learned is discouraged because it is brittle (the OS likes to close them) and uses resources permanently.

Disappointingly, it seems it is not possible to have a receiver in your manifest that intercepts SCREEN_ON events.

This has come up very recently:

android.intent.action.SCREEN_ON doesn't work as a receiver intent filter

also

Android - how to receive broadcast intents ACTION_SCREEN_ON/OFF?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...