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

最佳答案

我不知道我是否正确理解您,但似乎您需要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)) {

        }
    }

}

关于java - 如果手机从 sleep 模式返回时如何获取Android,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45371367/

10-13 04:40