Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 649
Jim Blackler Avatar asked Apr 04 '10 17:04

Jim Blackler


People also ask

What is difference between intent and intent filter in Android?

An intent is an object that can hold the os or other app activity and its data in uri form.It is started using startActivity(intent-obj).. \n whereas IntentFilter can fetch activity information on os or other app activities.

What is action in intent filter?

An intent filter is an expression in an app's manifest file that specifies the type of intents that the component would like to receive. For instance, by declaring an intent filter for an activity, you make it possible for other apps to directly start your activity with a certain kind of intent.

Can an activity have multiple intent filters?

However, since a component can have multiple intent filters, an intent that does not pass through one of a component's filters might make it through on another. An <intent-filter> element in the manifest file lists actions as <action> subelements. For example: <intent-filter . . . >

What is the difference between intent and broadcast receiver?

An intent is a messaging object, a broadcast receiver is an app component. An intent is used to request some action from some app component, it could be a broadcast receiver, an activity or a service.


1 Answers

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

like image 63
CommonsWare Avatar answered Sep 28 '22 13:09

CommonsWare