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
  • 11,025
  • 7
  • 57
  • 76
  • 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
  • 986,068
  • 189
  • 2,389
  • 2,491
1

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

Dharman
  • 30,962
  • 25
  • 85
  • 135
Ashu Singh
  • 341
  • 4
  • 7
0

maybe you forget to register the service on

<application> tag:

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

Acauã Pitta
  • 654
  • 1
  • 9
  • 16