覆盖在Android的电源按钮 [英] Overriding the power button in Android

查看:114
本文介绍了覆盖在Android的电源按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序中,我需要做一个动作时,电源按钮是pssed $ P $但遗憾的是我不能韩德尔电源按钮的作用时,它是pressed。我尝试使用的onkeydown()和dispatchKeyEvent()方法,但似乎没有奏效。任何人都可以给我建议任何其他方法或解决方案,这一问题。

 公共布尔的onkeydown(INT键code,KeyEvent的事件){
如果(event.getKey code()== KeyEvent.KEY code_POWER){
    //我想执行时,电源按钮pssed $ P $的操作。
    返回true;
}
返回super.onKeyDown(键code,事件);
}
 

  @覆盖
公共布尔dispatchKeyEvent(KeyEvent的事件){
如果(event.getKey code()== KeyEvent.KEY code_POWER){
    意图I =新的意图(这一点,NewActivity.class);
    startActivity(ⅰ);
    返回true;
}
返回super.dispatchKeyEvent(事件);
}
 

解决方案

我找到了答案,这可以通过使用AlarmManager与电源管理器

完成

  @覆盖
公共无效的onReceive(上下文的背景下,意图意图){
如果((intent.getAction()。等于(Intent.ACTION_SCREEN_OFF))){

电源管理器PM =(电源管理器)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);
意图INTEN =新的意图(背景下,NewActivity.class);
PendingIntent圆周率= PendingIntent.getActivity(上下文,0,INTEN,0);
alarmMgr.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,100,PI);
}
}

//这里完成你的WakeLock。调用此方法后ü把活动的前面,以及需要通过新的活动ü退出。
公共无效finishWakeLocker(){
如果(wakeLock!= NULL)
wakeLock.release();
}
 

  

下面先在屏幕熄灭,然后我通过AlarmManager唤醒它。通过重写的电源按钮控制,我不能停止屏幕会关闭状态。我只是醒来的设备尽快进入睡眠状态。

I'm developing an app in which I need to do an action when the power button is pressed but unfortunately I cant handel the action of power button when it is pressed. I tried using the onKeyDown() and dispatchKeyEvent() methods but nothing seems to be working. Can anyone suggest me any other method or solution for this problem.

public boolean onKeyDown(int keyCode, KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_POWER) {
    // The action I want to perform when power button is pressed.
    return true;
}
return super.onKeyDown(keyCode, event);
}

and

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_POWER) {
    Intent i = new Intent(this, NewActivity.class);
    startActivity(i);
    return true;
}
return super.dispatchKeyEvent(event);
}

解决方案

I found the answer and this can be done by using the AlarmManager with the PowerManager

@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();  
}

Here first the screen goes off and then I'm waking it by using the AlarmManager. I couldn't stop the screen going to off state by overriding the Power button controls. I'm just waking up the device as soon as it goes to sleep state.

这篇关于覆盖在Android的电源按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆