版权声明
本文来自博客园,作者:观心静 ,转载请注明原文链接:https://www.cnblogs.com/guanxinjing/p/17890899.html
本文版权归作者和博客园共有,欢迎转载,但必须给出原文链接,并保留此段声明,否则保留追究法律责任的权利。
前言
此博客基于Android10版本讲解如何在系统中添加自定义的系统全局广播。下面通过一个发送按键广播的例子在说明
例子:发送按键广播
系统广播添加位置
修改文件路径:/aosp/frameworks/base/core/res/AndroidManifest.xml
在PhoneWindowManager中添加发送广播的代码
修改文件路径:/aosp/frameworks/base/services/core/java/com/android/server/policy/PhoneWindowManager.java
在PhoneWindowManager的interceptKeyBeforeQueueing方法中添加下面注释中的代码
@Override
public int interceptKeyBeforeQueueing(KeyEvent event, int policyFlags) {
if (!mSystemBooted) {
// If we have not yet booted, don't let key events do anything.
return 0;
}
final boolean interactive = (policyFlags & FLAG_INTERACTIVE) != 0;
final boolean down = event.getAction() == KeyEvent.ACTION_DOWN;
final boolean canceled = event.isCanceled();
final int keyCode = event.getKeyCode();
final int displayId = event.getDisplayId();
final boolean isInjected = (policyFlags & WindowManagerPolicy.FLAG_INJECTED) != 0;
//略..........
// If the key would be handled globally, just return the result, don't worry about special
// key processing.
if (isValidGlobalKey(keyCode)
&& mGlobalKeyManager.shouldHandleGlobalKey(keyCode, event)) {
if (isWakeKey) {
wakeUp(event.getEventTime(), mAllowTheaterModeWakeFromKey,
PowerManager.WAKE_REASON_WAKE_KEY, "android.policy:KEY");
}
return result;
}
// Enable haptics if down and virtual key without multiple repetitions. If this is a hard
// virtual key such as a navigation bar button, only vibrate if flag is enabled.
final boolean isNavBarVirtKey = ((event.getFlags() & KeyEvent.FLAG_VIRTUAL_HARD_KEY) != 0);
boolean useHapticFeedback = down
&& (policyFlags & WindowManagerPolicy.FLAG_VIRTUAL) != 0
&& (!isNavBarVirtKey || mNavBarVirtualKeyHapticFeedbackEnabled)
&& event.getRepeatCount() == 0;
//===============在此处添加要发送的按键广播
Intent devKeyIntent = new Intent();
if (down) {
devKeyIntent.setAction("com.zhou.action.DEV_KEY_DOWN");
} else {
devKeyIntent.setAction("com.zhou.action.DEV_KEY_UP");
}
devKeyIntent.putExtra("keyCode", keyCode);
mContext.sendBroadcast(devKeyIntent);
//================
// Handle special keys.
switch (keyCode) {
case KeyEvent.KEYCODE_BACK: {
if (down) {
interceptBackKeyDown();
} else {
boolean handled = interceptBackKeyUp(event);
// Don't pass back press to app if we've already handled it via long press
if (handled) {
result &= ~ACTION_PASS_TO_USER;
}
}
break;
}
//略..............
在自己的应用中注册并且接收广播
在Activity中注册并且接收广播
class MainActivity : AppCompatActivity() {
lateinit var mReceiver: DevKeyReceiver
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//注册广播
val intentFilter = IntentFilter()
intentFilter.addAction("com.zhou.action.DEV_KEY_UP")
intentFilter.addAction("com.zhou.action.DEV_KEY_DOWN")
mReceiver = DevKeyReceiver()
registerReceiver(mReceiver, intentFilter)
}
override fun onDestroy() {
super.onDestroy()
//注销广播
unregisterReceiver(mReceiver)
}
}
class DevKeyReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
Log.e("zh", "接受广播 action = ${intent.action} keyCode = ${intent.getIntExtra("keyCode", -1)}")
Toast.makeText(context, "接受广播 action = ${intent.action} keyCode = ${intent.getIntExtra("keyCode", -1)}", Toast.LENGTH_SHORT).show()
}
}
效果图
这里可以通过adb命令发送各种键值以达到测试目的,例如adb shell input keyevent 24 音量加 和 adb shell input keyevent 25 音量减
end
本文来自博客园,作者:观心静 ,转载请注明原文链接:https://www.cnblogs.com/guanxinjing/p/17890899.html
本文版权归作者和博客园共有,欢迎转载,但必须给出原文链接,并保留此段声明,否则保留追究法律责任的权利。