How to Hook into the Power Button in Android

How to hook into the Power button in Android?

Solution:

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

return super.dispatchKeyEvent(event);
}

Want to Access Power Button events in android

You cannot override the power button press from your application. But when you press power button the screen turns on/off. So you can detect this using a broadcast receiver.

<receiver android:name=".MyBroadCastReciever">
<intent-filter>
<action android:name="android.intent.action.SCREEN_OFF"/>
<action android:name="android.intent.action.SCREEN_ON"/>
</intent-filter>
</receiver>

MyBroadCastReciever.java

public class MyBroadCastReciever extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
//Take count of the screen off position
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
//Take count of the screen on position
}
}
}

Hope this is helpful for you.

Note:-For to keep my broadcast receiver always running in Background.I am written one Background Service which continuously start in background and give boost to my broadcast receiver.

Overriding the power button in Android

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.

Play sound on power button pressed in android

To achieve that you can create receiver that is triggered when user presses power button and play music when that receiver is triggered:

public class MyBroadCastReciever extends BroadcastReceiver {

MediaPlayer mediaPlayer = null;

@Override
public void onReceive(Context context, Intent intent) {
mediaPlayer = MediaPlayer.create(context, R.raw.click123);
mediaPlayer.start();
}
}

Instantiate a reciever and register it within onCreate of your MainActivity:

MyBroadCastReciever myBroadCastReciever = new MyBroadCastReciever();
IntentFilter screenStateFilter = new IntentFilter();
screenStateFilter.addAction(Intent.ACTION_SCREEN_OFF);
registerReceiver(myBroadCastReciever, screenStateFilter);

And don't forget to unregister it inside onDestroy of your MainActivity:

unregisterReceiver(myBroadCastReciever);

activate an application when a power button is clicked

You can try this trick .

Register a Broadcast Receiver which is initiated when powerbutton is clicked.
Now in OnReceive method of the Receiver do what you want.

For example:

in manifest file register a receiver:

 <receiver android:name="com.test.check.MyReceiver">
<intent-filter>
<action android:name="android.intent.action.SCREEN_OFF"></action>
<action android:name="android.intent.action.SCREEN_ON"></action>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED"></action>
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"></action>
<action android:name="android.intent.action.ACTION_SHUTDOWN"></action>
</intent-filter>
</receiver>

&& in onReceive() method of the Receiver

 public class MyReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub

Log.v("#@%@%#", "Power button is pressed.");

Toast.makeText(arg0, "power button clicked",Toast.LENGTH_LONG).show();

//perform what you want here
}
}

Now perform any operation in onReceive() method of the Receiver.

How to hook into the Power button in Android?

Solution:

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

return super.dispatchKeyEvent(event);
}


Related Topics



Leave a reply



Submit