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