问题
I want to listen to power key event. How can I do that?
Currently the code I am using is this:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(KeyEvent.KEYCODE_POWER == event.getKeyCode()){
Log.e("POWER", "pow");
return true;//If event is handled, falseif
}
return super.onKeyDown(keyCode, event);
}
And the user permissions
<uses-permission android:name="android.permission.PREVENT_POWER_KEY" />
I do not get any result.
I want a add a functionality when user presses power button or long presses he can have an emergency call/ Speed dial.
Looking for a solution.
回答1:
Does it make a difference if you write
KeyEvent.KEYCODE_POWER == keyCode
? I think you should write this, and not
KeyEvent.KEYCODE_POWER == event.getKeyCode()
taken from here: How to hook into the Power button in Android?
回答2:
You should check in your override function onKeyDown
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_POWER) {
// Do something here...
return true;
}
return super.onKeyDown(keyCode, event);
}
and the same with onKeyLongPress if you using
Another way you would like to handle screen off, you can register BroadcastReceiver with action Intent.ACTION_SCREEN_OFF
回答3:
package app.test;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.Environment;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.provider.Settings;
import android.util.Log;
public class Test extends Activity implements SensorEventListener {
private WakeLock mWakelock = null;
private SensorManager mMgr;
private Sensor mAccel;
private BufferedWriter mLog;
final private SimpleDateFormat mTimeFormat = new SimpleDateFormat("HH:mm:ss - ");
private int mSavedTimeout;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mMgr = (SensorManager) this.getSystemService(SENSOR_SERVICE);
mAccel = mMgr.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
try {
String filename = Environment.getExternalStorageDirectory().getAbsolutePath() +"/accel.log";
mLog = new BufferedWriter(new FileWriter(filename, true));
}
catch(Exception e) {
e.printStackTrace();
finish();
}
PowerManager pwrMgr = (PowerManager) this.getSystemService(POWER_SERVICE);
mWakelock = pwrMgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Accel");
mWakelock.acquire();
try {
mSavedTimeout = Settings.System.getInt(getContentResolver(),
Settings.System.SCREEN_OFF_TIMEOUT);
}
catch(Exception e) {
mSavedTimeout = 120000;
}
Settings.System.putInt(getContentResolver(),
Settings.System.SCREEN_OFF_TIMEOUT, 5000);
}
public BroadcastReceiver mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_SCREEN_OFF.equals(intent.getAction())) {
writeLog("The screen has turned off");
mMgr.unregisterListener(Test.this);
mMgr.registerListener(Test.this, mAccel,
SensorManager.SENSOR_DELAY_NORMAL);
}
}
};
@Override
protected void onStart() {
mMgr.registerListener(this, mAccel, SensorManager.SENSOR_DELAY_NORMAL);
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
registerReceiver(mReceiver, filter);
super.onStart();
}
@Override
protected void onStop() {
mMgr.unregisterListener(this, mAccel);
unregisterReceiver(mReceiver);
try {
mLog.flush();
} catch (IOException e) {
}
super.onStop();
}
@Override
protected void onDestroy() {
try {
mLog.flush();
mLog.close();
}
catch(Exception e) {
}
Settings.System.putInt(getContentResolver(),
Settings.System.SCREEN_OFF_TIMEOUT, mSavedTimeout);
mWakelock.release();
super.onDestroy();
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
public void onSensorChanged(SensorEvent event) {
writeLog("Got a sensor event: " + event.values[0] + ", " +
event.values[1] + ", " + event.values[2]);
}
private void writeLog(String str) {
try {
Date now = new Date();
mLog.write(mTimeFormat.format(now));
mLog.write(str);
mLog.write("\n");
}
catch(IOException ioe) {
ioe.printStackTrace();
}
}
}
Enjoyyyy
来源:https://stackoverflow.com/questions/19180659/power-key-event-in-android