AndroidR 实现长按按键打开一个app或者打开app的某个界面
一、frameworks\base\services\core\java\com\android\server\policy\PhoneWindowManager.java interceptKeyBeforeDispatching 里面处理按键长按事件
二、接收按键长按事件并然后用startActivityAsUser打开app
三、接收按键长按事件然后用startActivityAsUser打开某个app的某个页面
3.1 先在要被跳转的activity里面配置好 intent-filter参数
<intent-filter>
<action android:name="android.intent.action.IRThermal" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
3.2 执行startActivityAsUser(new Intent("android.intent.action.IRThermal"),
UserHandle.CURRENT_OR_SELF); 跳转到对应的页面。
扫描二维码关注公众号,回复:
14502980 查看本文章
3.3 长按按键一秒后打开app的某个界面
3.4 查看相应的log
四、使用Scheme跳转,什么是 URL Scheme? android中的scheme是一种页面内跳转协议,是一种非常好的实现机制,通过定义自己的scheme协议,可以非常方便跳转app中的各个页面;通过scheme协议,服务器可以定制化告诉App跳转那个页面,可以通过通知栏消息定制化跳转页面
startActivityAsUser(new Intent("android.intent.action.IRThermal",Uri.parse("giada://jh558")),
UserHandle.CURRENT_OR_SELF);
<activity android:name=".sample.SerialPortPreferences" >
<intent-filter>
<action android:name="android.intent.action.IRThermal" />
<category android:name="android.intent.category.DEFAULT" />
<data
android:host="jh558"
android:scheme="giada" />
</intent-filter>
</activity>
五、整个过程的所有修改内容如下
diff --git a/services/core/java/com/android/server/policy/PhoneWindowManager.java b/services/core/java/com/android/server/policy/PhoneWindowManager.java
index d966f36..cd99647 100755
--- a/services/core/java/com/android/server/policy/PhoneWindowManager.java
+++ b/services/core/java/com/android/server/policy/PhoneWindowManager.java
@@ -229,6 +229,7 @@ import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashSet;
import java.util.List;
+import android.net.Uri;
/**
* WindowManagerPolicy implementation for the Android phone UI. This
@@ -636,6 +637,7 @@ public class PhoneWindowManager implements WindowManagerPolicy {
private static final int MSG_LAUNCH_ASSIST_LONG_PRESS = 24;
private static final int MSG_POWER_VERY_LONG_PRESS = 25;
private static final int MSG_RINGER_TOGGLE_CHORD = 26;
+ private static final int MSG_PHOTO_VERY_LONG_PRESS = 27;
private int screenWidth;
private int screenHeight;
@@ -731,6 +733,21 @@ public class PhoneWindowManager implements WindowManagerPolicy {
case MSG_POWER_LONG_PRESS:
powerLongPress();
break;
+ case MSG_PHOTO_VERY_LONG_PRESS:
+ /*
+ Intent intent = new Intent();
+ intent = mPackageManager.getLaunchIntentForPackage("com.topdon.tc003");
+ startActivityAsUser(intent, UserHandle.CURRENT);
+ */
+
+ /*startActivityAsUser(new Intent("android.intent.action.IRThermal"),
+ UserHandle.CURRENT_OR_SELF);
+ */
+ startActivityAsUser(new Intent("android.intent.action.IRThermal",Uri.parse("giada://jh558")),
+ UserHandle.CURRENT_OR_SELF);
+
+ Log.d(TAG, "received MSG_PHOTO_VERY_LONG_PRESS message , go to open com.topdon.tc003 app IRThermal! ");
+ break;
case MSG_POWER_VERY_LONG_PRESS:
powerVeryLongPress();
break;
@@ -2852,11 +2869,14 @@ public class PhoneWindowManager implements WindowManagerPolicy {
Slog.wtf(TAG, "KEYCODE_VOICE_ASSIST should be handled in interceptKeyBeforeQueueing");
return -1;
} else if (keyCode == KeyEvent.KEYCODE_SYSRQ) {
+ Slog.wtf(TAG, "KeyEvent.KEYCODE_SYSRQ repeatCount "+repeatCount+" down:"+down);
if (down && repeatCount == 0) {
- mScreenshotRunnable.setScreenshotType(TAKE_SCREENSHOT_FULLSCREEN);
- mScreenshotRunnable.setScreenshotSource(SCREENSHOT_KEY_OTHER);
- mHandler.post(mScreenshotRunnable);
- }
+ Slog.wtf(TAG, "KeyEvent.KEYCODE_SYSRQ down & repeatCount = 0");
+ mHandler.sendEmptyMessageDelayed(MSG_PHOTO_VERY_LONG_PRESS, 1000);
+ }else if (!down){
+ Slog.wtf(TAG, "KeyEvent.KEYCODE_SYSRQ down false");
+ mHandler.removeMessages(MSG_PHOTO_VERY_LONG_PRESS);
+ }
return -1;
} else if (keyCode == KeyEvent.KEYCODE_BRIGHTNESS_UP
|| keyCode == KeyEvent.KEYCODE_BRIGHTNESS_DOWN) {
六、有价值的参考文章