【问题标题】:how to get if phone come back from sleep mode Android如何获取手机是否从睡眠模式返回 Android
【发布时间】:2017-07-28 10:41:18
【问题描述】:

如果手机从睡眠模式恢复,是否可以接收?我需要一个接收器。 OnScreenOn 或 interactive 是检查屏幕是打开还是关闭。但我需要准确地知道屏幕何时从睡眠模式恢复。安卓。 API 23 及更高版本。

【问题讨论】:

  • 如果手机从睡眠模式恢复,是否需要等待接收?我需要一个接收器。 OnScreenOn 或 interactive 是检查屏幕是打开还是关闭。但我需要准确地知道屏幕何时从睡眠模式恢复。安卓。 API 23 及更高版本。
  • 什么手机?你是机器人吗?
  • 三星 s6 edge Android 7.0-7.1.2
  • onResume 怎么样?
  • 恢复?我把它放在接收器里了吗?活动?

标签: java android


【解决方案1】:

我不知道我是否理解正确,但您似乎需要BroadcastReceiver

你有更多信息this answer

为了完整起见,我将在此处添加:

将以下内容添加到您的清单中:

<receiver android:name=".UserPresentBroadcastReceiver">
  <intent-filter>
    <action android:name="android.intent.action.USER_PRESENT" />
    <action android:name="android.intent.action.ACTION_SHUTDOWN" />
 </intent-filter>
</receiver>

处理动作:

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class UserPresentBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context arg0, Intent intent) {

        /*Sent when the user is present after 
         * device wakes up (e.g when the keyguard is gone)
         * */
        if(intent.getAction().equals(Intent.ACTION_USER_PRESENT)){

        }
        /*Device is shutting down. This is broadcast when the device 
         * is being shut down (completely turned off, not sleeping)
         * */
        else if (intent.getAction().equals(Intent.ACTION_SHUTDOWN)) {

        }
    }

}

【讨论】:

  • 您好,但问题是它在设备重新启动一次后才能工作。
猜你喜欢
  • 1970-01-01
  • 2016-05-17
  • 1970-01-01
  • 2012-04-24
  • 1970-01-01
  • 1970-01-01
  • 2014-05-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多