简体   繁体   English

将关键事件分派到自己的应用程序内的通用上下文

[英]Dispatch key event to a generic context inside the own app

I am developing and Android app (in Java) that needs to receive a broadcast from another application and, according to the data received, dispatch some key events inside my own app.我正在开发一个需要从另一个应用程序接收广播的 Android 应用程序(Java),并根据收到的数据在我自己的应用程序中调度一些关键事件。 Basically, from inside my app, I need to control multiple Views and Activities to navigate through my interface using the key events.基本上,从我的应用程序内部,我需要控制多个视图和活动,以使用键事件在我的界面中导航。

I am trying the dispatchKeyEvent method that exists in both Activity and View.我正在尝试 Activity 和 View 中都存在的 dispatchKeyEvent 方法。 Below are the documentations:以下是文档:

https://developer.android.com/reference/android/view/View#dispatchKeyEvent(android.view.KeyEvent) https://developer.android.com/reference/android/view/View#dispatchKeyEvent(android.view.KeyEvent)

https://developer.android.com/reference/android/app/Activity#dispatchKeyEvent(android.view.KeyEvent) https://developer.android.com/reference/android/app/Activity#dispatchKeyEvent(android.view.KeyEvent)

The problem is: this function only dispatches events to be listened by objects inside the same View or Activity that generated the dispatch.问题是:这个函数只调度要被生成调度的同一视图或活动内的对象监听的事件。 Considering that my app has multiple Activities and Views, I cannot properly navigate because not all contexts are able to listen to these events.考虑到我的应用程序有多个活动和视图,我无法正确导航,因为并非所有上下文都能够监听这些事件。 For example, I am able to control my homescreen, but as soon as I go to another screen, it stops controlling.例如,我可以控制我的主屏幕,但一旦我转到另一个屏幕,它就会停止控制。

Is there a way of sending key events from my app to itself in a generic way, so that all context inside the app is able to receive these events?有没有办法以通用方式将关键事件从我的应用程序发送到自身,以便应用程序内的所有上下文都能够接收这些事件?

Important: I cannot have root privileges.重要提示:我不能拥有 root 权限。

Thanks for your time!谢谢你的时间!

You're making your life harder on yourself.你让自己的生活变得更加艰难。 Forget key events.忘记关键事件。 That's not the correct way to do anything on Android.这不是在 Android 上做任何事情的正确方法。 And especially not to do anything across Activites.尤其是不要跨 Activites 做任何事情。 Instead, use something like an RxJava Observable to send events (NOT key events, specific events for things you want to do) and have each activity subscribe to that observable when in the foreground and translate incoming events to commands on that view.相反,使用 RxJava Observable 之类的东西来发送事件(不是关键事件,您想要做的事情的特定事件)并让每个活动在前台订阅该 observable 并将传入事件转换为该视图上的命令。

Honestly, even if you could send key events you wouldn't want to.老实说,即使您可以发送您不想发送的关键事件。 You would then have your API to external apps dependent on a specific implementation of a screen in some random activity that you don't know what its doing.然后,您将在一些您不知道它在做什么的随机活动中将您的 API 用于依赖于特定屏幕实现的外部应用程序。 That a massive snarl of bad code and mistakes waiting to happen.大量糟糕的代码和错误等待发生。 Instead, dispatch high level events and let the view itself turn those into view function calls so that all the translation is tightly coupled to the UI implementation, and not to the external API.相反,调度高级事件并让视图本身将这些事件转换为视图函数调用,以便所有转换都与 UI 实现紧密耦合,而不是与外部 API。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM