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

╄→гoц情女王★ 提交于 2019-12-17 06:47:04

问题


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.


回答1:


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".




回答2:


This is the best example I've found http://androidexample.com/Screen_Wake_Sleep_Event_Listner_Service_-_Android_Example/index.php?view=article_discription&aid=91&aaid=115




回答3:


Actullay i was faceing this issue but i resolve it succeessfully

1) start service from your main activity

   Intent i = new Intent(MainActivity.this, UpdateService.class);
    startService(i);

2) register reciver in service class.

@Override
public void onCreate() {
    super.onCreate();



    // REGISTER RECEIVER THAT HANDLES SCREEN ON AND SCREEN OFF LOGIC
    IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
    filter.addAction(Intent.ACTION_SCREEN_OFF);
    BroadcastReceiver mReceiver = new ScreenReciever();
    registerReceiver(mReceiver, filter);
}

3) Done



来源:https://stackoverflow.com/questions/2575242/android-intent-action-screen-on-doesnt-work-as-a-receiver-intent-filter

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