唤醒Android设备

嘿,我需要在特定时间唤醒我正在睡眠的android设备。有什么建议么?

PS唤醒:打开显示屏,甚至可以解锁手机


波斯汪
浏览 459回答 3
3回答

大话西游666

我找到了一种方法,它并不那么复杂...适用于任何API版本。您需要使用PowerManager.userActivity(l, false)方法并将您的活动注册为针对SCREEN_OFF意向的广播:在您的活动中,OnCreate放置了以下内容:mReceiver = new BroadcastReceiver() {&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onReceive(Context context, Intent intent) {&nbsp; &nbsp; &nbsp; &nbsp; Log.v(TAG, "Screen OFF onReceive()");&nbsp; &nbsp; &nbsp; &nbsp; screenOFFHandler.sendEmptyMessageDelayed(0, 2000L);&nbsp; &nbsp; }};屏幕关闭事件2秒后,它将启动处理程序。在您的onResume()方法中注册接收者:IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);registerReceiver(mReceiver, filter);Log.i(TAG, "broadcast receiver registered!");创建一个类似于以下内容的处理程序:private Handler screenOFFHandler = new Handler() {&nbsp; &nbsp; @Override&nbsp; &nbsp; public void handleMessage(Message msg) {&nbsp; &nbsp; &nbsp; &nbsp; super.handleMessage(msg);&nbsp; &nbsp; &nbsp; &nbsp; // do something&nbsp; &nbsp; &nbsp; &nbsp; // wake up phone&nbsp; &nbsp; &nbsp; &nbsp; Log.i(TAG, "ake up the phone and disable keyguard");&nbsp; &nbsp; &nbsp; &nbsp; PowerManager powerManager = (PowerManager) YourActivityName.this&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .getSystemService(Context.POWER_SERVICE);&nbsp; &nbsp; &nbsp; &nbsp; long l = SystemClock.uptimeMillis();&nbsp; &nbsp; &nbsp; &nbsp; powerManager.userActivity(l, false);//false will bring the screen back as bright as it was, true - will dim it&nbsp; &nbsp; }};在清单文件中请求权限:<uses-permission android:name="android.permission.WAKE_LOCK" />完成后,不要忘记注销广播接收器。例如,您可以在onDestroy()中这样做(不保证)unregisterReceiver(mReceiver);Log.i(TAG, "broadcast UNregistred!");
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Android