如何挂钩在Android的电源button?

在Android设备上,只有音量button和电源button的情况下,我想让应用程序按下电源button(长短)。 这是怎么做的?

Related of "如何挂钩在Android的电源button?"

现有的答案并没有完全回答这个问题,并留下了足够的细节,没有更多的调查,他们将无法工作。 我将分享我所学到的解决这个问题。

首先,您需要将以下权限添加到清单文件中:

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

要处理短按和长按,请将以下替代操作添加到您的活动课程中:

 @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_POWER) { // Do something here... event.startTracking(); // Needed to track long presses return true; } return super.onKeyDown(keyCode, event); } @Override public boolean onKeyLongPress(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_POWER) { // Do something here... return true; } return super.onKeyLongPress(keyCode, event); } 

注意:值得注意的是,onKeyDown()会在onKeyLongPress之前触发多次,所以您可能需要在onKeyUp()或其他逻辑上触发,以防止在用户真正按住onKeyDown() 。

我认为接下来的部分只针对Cyanogenmod。 如果 PREVENT_POWER_KEY 常量是未定义的,那么你不应该需要它。

要开始拦截电源键,您需要在活动中设置以下标志:

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

停止拦截电源键(允许标准function):

 getWindow().clearFlags(WindowManager.LayoutParams.PREVENT_POWER_KEY); 

如果您愿意,您可以在程序中反复切换两种模式。

解:

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

在你的活动上添加:

 public boolean onKeyDown(int keyCode, KeyEvent event) { if (event.getKeyCode() == KeyEvent.KEYCODE_POWER) { // do what you want with the power button return true; } return super.onKeyDown(keyCode, event); } 

虽然…这种钥匙是特别的…不知道它是否会给你带来麻烦。

正如这里所说的https://stackoverflow.com/a/15828592/1065357

 @Override public void onWindowFocusChanged(boolean hasFocus) { super.onWindowFocusChanged(hasFocus); if(!hasFocus) { Intent closeDialog = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS); sendBroadcast(closeDialog); } } 

共享一个方法来听电源button长按。 适用于API 23+权限:

  1. 请求系统权限绘制覆盖 (这不是一个正常或易受攻击的权限)。 这不是一个用户权限,所以你应该真的知道,你在做什么,要求。

     public class MainActivity extends AppCompatActivity { public final static int REQUEST_CODE = 10101; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if (checkDrawOverlayPermission()) { startService(new Intent(this, PowerButtonService.class)); } } public boolean checkDrawOverlayPermission() { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) { return true; } if (!Settings.canDrawOverlays(this)) { /** if not construct intent to request permission */ Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + getPackageName())); /** request permission via start activity for result */ startActivityForResult(intent, REQUEST_CODE); return false; } else { return true; } } @Override @TargetApi(Build.VERSION_CODES.M) protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == REQUEST_CODE) { if (Settings.canDrawOverlays(this)) { startService(new Intent(this, PowerButtonService.class)); } } } } 
  2. 启动一个服务并添加一个特殊的视图到WindowManager

  3. 等待ViewonCloseSystemDialogs方法中的一个动作。

     public class PowerButtonService extends Service { public PowerButtonService() { } @Override public void onCreate() { super.onCreate(); LinearLayout mLinear = new LinearLayout(getApplicationContext()) { //home or recent button public void onCloseSystemDialogs(String reason) { if ("globalactions".equals(reason)) { Log.i("Key", "Long press on power button"); } else if ("homekey".equals(reason)) { //home key pressed } else if ("recentapss".equals(reason)) { // recent apps button clicked } } @Override public boolean dispatchKeyEvent(KeyEvent event) { if (event.getKeyCode() == KeyEvent.KEYCODE_BACK || event.getKeyCode() == KeyEvent.KEYCODE_VOLUME_UP || event.getKeyCode() == KeyEvent.KEYCODE_VOLUME_DOWN || event.getKeyCode() == KeyEvent.KEYCODE_CAMERA || event.getKeyCode() == KeyEvent.KEYCODE_POWER) { Log.i("Key", "keycode " + event.getKeyCode()); } return super.dispatchKeyEvent(event); } }; mLinear.setFocusable(true); View mView = LayoutInflater.from(this).inflate(R.layout.service_layout, mLinear); WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE); //params WindowManager.LayoutParams params = new WindowManager.LayoutParams( 100, 100, WindowManager.LayoutParams.TYPE_SYSTEM_ALERT, WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_FULLSCREEN | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, PixelFormat.TRANSLUCENT); params.gravity = Gravity.LEFT | Gravity.CENTER_VERTICAL; wm.addView(mView, params); } @Override public IBinder onBind(Intent intent) { return null; } } 

performance:

 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="powerbuttonpress"> <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> <service android:name=".PowerButtonService" android:enabled="true" android:exported="true"> </service> </application> </manifest> 

service_layout:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> </LinearLayout> 

您可以覆盖Activity类中的public boolean onKeyDown(int keyCode, KeyEvent event)public boolean onKeyUp(int keyCode, KeyEvent event)函数,并testingkeyCode是否等于KeyEvent.KEYCODE_POWER

我没有testing过这个,但是我会假定系统把它看作是Home键,因为你不能阻止系统接收到关键事件,你只能观察到它发生了。 为了testing这个,试着从上面的函数返回True ,看看这个是否捕获到关键事件。

你必须使用这个:

 BroadcastReceiver screenoff = new BroadcastReceiver() { public static final String Screenoff = "android.intent.action.SCREEN_OFF"; @Override public void onReceive(Context context, Intent intent) { if (!intent.getAction().equals(Screenoff)) return; //put code to handle power press here return; }};