Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

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

I'm trying to get a BroadcastReceiver invoked when the screen is turned on. In my AndroidManifest.xml I have specified :

                <receiver android:name="IntentReceiver">
                    <intent-filter>
                            <action android:name="android.intent.action.SCREEN_ON"></action>
                    </intent-filter>
                </receiver>

However it seems the receiver is never invoked (breakpoints don't fire, log statements ignored). I've swapped out SCREEN_ON for BOOT_COMPLETED for a test, and this does get invoked.

This is in a 1.6 (SDK level 4) project.

A Google Code Search revealed this, I downloaded the project and synced it, converted it to work with latest tools, but it too is not able to intercept that event.

http://www.google.com/codesearch/p?hl=en#_8L9bayv7qE/trunk/phxandroid-intent-query/AndroidManifest.xml&q=android.intent.action.SCREEN_ON

Is this perhaps no longer supported?

Previously I have been able to intercept this event successfully with a call to Context.registerReceiver() like so

registerReceiver(new BroadcastReceiver() {

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

However this was performed by a long-living Service. Following sage advice from CommonsWare I have elected to try to remove the long-living Service and use different techniques. But I still need to detect the screen off and on events.

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

Following sage advice from CommonsWare I have elected to try to remove the long-living Service and use different techniques.

Actually, I believe my advice was more of a light blue... :-)

But I still need to detect the screen off and on events.

There are certain events that Android does not want to start up new processes for, so the device does not get too slow from all sorts of stuff all having to run at once. ACTION_SCREEN_ON is one of those. See this previous question for light blue advice on that topic.

So, you need to ask yourself, "Self, do I really need to get control on those events?". The core Android team would like it if your answer was "no".


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...