0

I am trying to make my app print something to log when screen is turned on but it doesn't work as I expected.

Here is what I have in my Manifest file

<application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity ...>
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>


    <receiver android:name="PhoneBroadcastReceiver" android:enabled="true">
        <intent-filter>
            <action android:name="android.intent.action.SCREEN_ON"></action>
        </intent-filter>
    </receiver>
</application>

and my receiver looks like

public class PhoneBroadcastReceiver extends BroadcastReceiver {

public PhoneBroadcastReceiver()
{
}

@Override
public void onReceive(Context _context, Intent _intent) {
    // TODO Auto-generated method stub
    String a = _intent.getAction();


    MessageHandler.log("Received action: " + a); // just a wrapper for printing to a log

}

}

but it prints nothing to the log. I keep pressing my Android power button and the screen turns on / turns off but my message doesn't appear in the log. What am I missing? It looks the same as in examples I found on the web.

Pijusn
  • 10,635
  • 7
  • 54
  • 75
  • Have you tried first to log something to the console of DDMS to see if it's really entering your broadcastReceiver? (with `Log.info("PhoneBroadcastReceiver ","Entering broadcast receiver")`. If you can see this log to the console then, the problem is in your MessageHandler class – ccheneson Jul 14 '11 at 19:33

3 Answers3

7

You cannot listen for ACTION_SCREEN_ON broadcasts via a BroadcastReceiver registered in the manifest. You have to register it via registerReceiver() from a running component. There are fairly few broadcasts with this trait (ACTION_SCREEN_OFF, ACTION_BATTERY_CHANGED, and perhaps ACTION_USER_PRESENT).

CommonsWare
  • 954,112
  • 185
  • 2,315
  • 2,367
1

ACTION_SCREEN_ON won't work if registered via manifest file. You need to register it dynamically.

Dharman
  • 26,923
  • 21
  • 73
  • 125
Ashu Singh
  • 331
  • 4
  • 7
0

maybe you forget to register the service on

<application> tag:

<service android:name=".YourService" />

Acauã Pitta
  • 577
  • 7
  • 15