Android:检测设备何时唤醒?

时间:2014-03-18 13:48:04

标签: android broadcastreceiver wakeup

我知道名为 WakefulBroadcastReceiver 的v4助手类,它是为了响应设备唤醒。

我想写一个无头的Android应用程序,它只是检测到设备已经唤醒,然后执行我想要的任何逻辑(在下面的测试应用程序中,它只是记录一条消息)。

但是,我无法在应用的清单中找出要指定的Intent,以便我的 WakefulBroadcastReceiver 会被解雇。

有没有人知道如何配置这样的应用程序,以便 WakefulBroadcastReceiver 检测到所有设备唤醒实例?

首先,这是 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));
    }
}

...这是运行的服务:

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);        
    }

}

最后,这是我的清单。注意" WHAT_GOES_HERE ????"在意图过滤器中。

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

非常感谢。

1 个答案:

答案 0 :(得分:1)

这个问题差不多有一年了,你有没有想过这个问题?我认为您在意图过滤器位中所需的操作是android.intent.action.SCREEN_ONandroid.intent.action.SCREEN_Off。我正在尝试做类似的事情,但发现由于一些神秘的原因,这些行为无法在清单中注册。