【问题标题】:Android: Detecting when device wake-up has occurred?Android:检测设备何时唤醒?
【发布时间】:2014-03-18 13:48:03
【问题描述】:

我知道名为 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>

非常感谢。

【问题讨论】:

标签: android broadcastreceiver wakeup


【解决方案1】:

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

【讨论】:

  • 不,很遗憾,我从来没有想过这个问题。这似乎是 Android 中不可能做到的事情之一(叹气)。还是我还缺少什么?
  • 无赖。我正在使用它将慢速轮询更改为小部件数据的快速轮询,手机睡着时慢,醒时快,用户更有可能看到我的小部件。我的下一个计划是使用android.intent.action.USER_PRESENT 广播从慢到快,虽然它可以在 XML 中注册,但在禁用键盘保护的情况下不会触发。为了转换回慢速,我将在轮询警报处理程序中检查屏幕状态。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-23
  • 1970-01-01
相关资源
最近更新 更多