1

I know about the v4 helper class called WakefulBroadcastReceiver, which is meant to respond to a device wake-up.

I want to write a headless Android app which simply detects that the device has woken up, and then performs whatever logic I desire (in the test app, below, it simply logs a message).

However, I couldn't find out what Intent(s) to specify in the app's manifest, so that my WakefulBroadcastReceiver will get fired off.

Does anyone know how to configure such an app, so that a WakefulBroadcastReceiver detects all instances of device wake-up?

First, here is the WakefulBroadcastReceiver:

public class MyWakeupReceiver extends WakefulBroadcastReceiver {

    public void onReceive(Context context, Intent intent) {

        Log.i("MyWakeupReceiver", "received wake-up intent: " + intent.toString());

        startWakefulService(context, new Intent(context, MyWakeupService.class));
    }
}

... and here is the service that gets run:

public class MyWakeupService extends IntentService {

    public MyWakeupService() {
        super("MyWakeupService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {

        Log.i("MyWakeupService", "onHandleIntent: " + intent.toString());

        // Do stuff here.

        MyWakeupReceiver.completeWakefulIntent(intent);        
    }

}

Finally, here is my manifest. Note "WHAT_GOES_HERE????" in the intent filter.

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="my.test.package"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="19"
        android:targetSdkVersion="19" />

    <uses-permission android:name="android.permission.WAKE_LOCK" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <receiver android:name="my.test.package.MyWakeupReceiver" android:enabled="true" android:exported="false">

            <intent-filter>
                <action android:name="android.intent.action.WHAT_GOES_HERE????" />
            </intent-filter>

        </receiver>

        <service android:name="my.test.package.MyWakeupService" />

    </application>

</manifest>

Thank you very much.

HippoMan
  • 2,119
  • 2
  • 25
  • 48
  • _WakefulBroadcastReceiver, which is meant to respond to a device wake-up_ This is not correct. Please see this: http://stackoverflow.com/a/26380942/1911652 – Atul May 24 '16 at 14:35

1 Answers1

1

This question is almost a year old, did you ever figure this out? I think the actions you wanted in the intent filter bit were android.intent.action.SCREEN_ON and android.intent.action.SCREEN_Off. I'm trying to do something similar, but have discovered that for some mysterious reason those actions can't be registered in the manifest.

William T. Mallard
  • 1,562
  • 2
  • 25
  • 33
  • No, unfortunately, I never figured this out. This seems to be one of the things that are not possible in Android (sigh). Or am I still missing something? – HippoMan Jan 31 '15 at 16:14
  • Bummer. I'm using it to change a slow poll to a fast poll for widget data, slow when the phone's asleep, fast when it's awake and the user is more likely to see my widget. My next plan is to use the `android.intent.action.USER_PRESENT` broadcast to go from slow to fast which, while it can be registered in XML it doesn't fire in cases where the keyguard is disabled. For the transition back to slow I'll just check the screen status in the poll alarm handler. – William T. Mallard Jan 31 '15 at 16:28