4

我试图识别当设备进入打盹模式时我们在 Android 中获得的单个可靠回调。我尝试了以下方法:

IntentFilter filter = new IntentFilter();
filter.addAction(PowerManager.ACTION_DEVICE_IDLE_MODE_CHANGED);
registerReceiver(new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d("ajitha", "intent action=" + intent.getAction()
                + " idleMode=" + pm.isDeviceIdleMode());
    }
}, filter);
super.onResume();

在上面的代码示例中,当设备打瞌睡时,接收器会被多次击中。在我的试运行中,logcat 显示如下:

    Line 103: 08-25 15:41:21.814 I/PowerManagerService(  845): Going to sleep due to power button (uid 1000)...
    Line 114: 08-25 15:41:22.601 I/PowerManagerService(  845): Dozing...
    Line 144: 08-25 15:41:36.834 D/ajitha  ( 6985): intent action=android.os.action.DEVICE_IDLE_MODE_CHANGED idleMode=true
    Line 146: 08-25 15:41:37.458 D/ajitha  ( 6985): intent action=android.os.action.DEVICE_IDLE_MODE_CHANGED idleMode=false
    Line 150: 08-25 15:41:38.268 D/ajitha  ( 6985): intent action=android.os.action.DEVICE_IDLE_MODE_CHANGED idleMode=true
    Line 154: 08-25 15:42:08.669 D/ajitha  ( 6985): intent action=android.os.action.DEVICE_IDLE_MODE_CHANGED idleMode=false
    Line 156: 08-25 15:42:09.287 D/ajitha  ( 6985): intent action=android.os.action.DEVICE_IDLE_MODE_CHANGED idleMode=true
    Line 175: 08-25 15:42:58.935 I/PowerManagerService(  845): Waking up from dozing (uid 1000)...
    Line 200: 08-25 15:42:59.273 D/ajitha  ( 6985): intent action=android.os.action.DEVICE_IDLE_MODE_CHANGED idleMode=false

如果意图被多次击中,我将无法根据此意图执行我的操作。是否有任何其他意图可以用来识别进入打瞌睡的设备???

为什么这个意图会被击中这么多次???

4

0 回答 0