如何知道Android手机会睡觉? 请帮我一个示例代码。 谢谢你的阅读。
答案 0 :(得分:1)
您需要为Intent.ACTION_SCREEN_OFF注册广播接收器
所以,创建broadcastreceiver
这是您处理screen_off意图的地方。
private BroadcastReceiver receiver = new BroadcastReceiver() {
public void onReceive(final Context context, final Intent intent) {
/*
* dispatch screen_off
* to handler method
*/
String iAction = intent.getAction();
if (iAction.equals(Intent.ACTION_SCREEN_OFF))
handleScreenAction(iAction);
}
};
现在注册接收器的过滤器。
static void registerReciever() {
IntentFilter myFilter = new IntentFilter();
// Catch screen off event
myFilter.addAction(Intent.ACTION_SCREEN_OFF);
registerReceiver(receiver, myFilter);
}