Android 应用程序运行时电源按钮覆盖和禁用

Android power button override and disable while application runing

嗨,我需要在应用程序 运行 时让设备屏幕保持正常状态。 禁用电源按钮功能以关闭屏幕。 我尝试了以下代码

getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);

和唤醒锁

@Override
public void onReceive(Context context, Intent intent) {
       if ((intent.getAction().equals(Intent.ACTION_SCREEN_OFF))) {

            PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
            wakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.ON_AFTER_RELEASE, "TEST");
            wakeLock.acquire();

            AlarmManager alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
            Intent inten = new Intent(context,NewActivity.class);
            PendingIntent pi = PendingIntent.getActivity(context, 0, inten, 0);
            alarmMgr.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,  100, pi);
            }
       }   

 // Finish your WakeLock HERE. call this method after U put the activity in front or when u exit from the new activity.
    public void finishWakeLocker(){
                  if (wakeLock != null) 
                  wakeLock.release();  
    }

提前致谢

出于安全原因,这是不可能的。如果没有系统级权限,HOME 和 POWER 按钮是您无法直接覆盖的两件事。毕竟,用户不希望您的应用程序控制他们的设备,对吗? ;-)

您应该以唤醒锁或

的方式规划功能
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

够了:-)

编辑:

有链接的one uday之类的教程,但是效果一般不可靠,只能在某些设备上使用。我已经开发了一个这样的应用程序并且相信我,你不想调试它;-)