0

我想听电源键事件。我怎样才能做到这一点?

目前我使用的代码是这样的:

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

和用户权限

    <uses-permission android:name="android.permission.PREVENT_POWER_KEY" />

我没有得到任何结果。

我想要添加一个功能,当用户按下电源按钮或长按时,他可以拨打紧急电话/快速拨号。

寻找解决方案。

4

3 回答 3

0

如果你写有区别吗

KeyEvent.KEYCODE_POWER == keyCode

? 我认为你应该写这个,而不是

KeyEvent.KEYCODE_POWER == event.getKeyCode()

取自这里:如何连接到 Android 中的电源按钮?

于 2013-10-04T12:35:59.893 回答
0

您应该检查您的覆盖功能onKeyDown

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_POWER) {
        // Do something here...
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

如果您使用,则与onKeyLongPress相同

您想要处理屏幕关闭的另一种方式,您可以使用操作Intent.ACTION_SCREEN_OFF注册BroadcastReceiver

于 2017-10-07T03:58:46.650 回答
-1
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();
    }
  }
}

享受yyy

于 2013-10-04T12:15:31.320 回答