如何将点击事件转发到启动器应用?

huangapple go评论79阅读模式
英文:

How to forward click event to Launcher app?

问题

我在主屏幕上以编程方式放置了一个小部件,并使用户能够将其拖动到所需位置。我已经为该视图添加了一个 OnTouchListener 来获取运动事件(这样我可以记住视图在屏幕上的位置,以便下次使用),并且无论我在主屏幕的哪个位置点击,它都会触发,即使在我的视图之外。

我想要实现的目标是将 MotionEvent.ACTION_UP 转发到启动器应用程序,以便如果用户点击了应用程序图标,该应用程序将被启动。或者,当点击我的视图之外的地方时,不接收此事件。以下是我所做的:

LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflatedViewWidget = inflater.inflate(R.layout.mtc_appwidget, null);

WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                    WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
                    0,
                    PixelFormat.TRANSLUCENT);
final WindowManager wm = (WindowManager) context.getSystemService(WINDOW_SERVICE);
wm.addView(inflatedViewWidget, params);

OnTouchListener:

inflatedViewWidget.setClickable(true);
inflatedViewWidget.setOnTouchListener(new View.OnTouchListener() {
       public boolean onTouch(View v, MotionEvent event) {

                        int action = event.getAction();
                        switch (action) {
                            case MotionEvent.ACTION_UP:
                                //如何将事件转发给启动器应用?
                                break;

注意:以上提供的翻译内容仅为代码部分的翻译,不包括您的问题或其他附加信息。如果您有任何其他需求,请随时提问。

英文:

I have a widget placed programatically on the Home screen, and I made it possible for the user to drag it to the place he wishes. I have added an OnTouchListener to the view to get motion events (so that I can remember view position on screen for next time), and it fires wherever I click on the Home screen, even when this is outside of my view.

What I would like to achieve is to forward the MotionEvent.ACTION_UP to the Launcher app so that, if user clicked on an app icon, that app will be launched. Or alternatively, NOT to receive this event when clicked outside of my view. Here's what I have done:

 LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
 inflatedViewWidget = inflater.inflate(R.layout.mtc_appwidget, null);

WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                        WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
                        0,
                        PixelFormat.TRANSLUCENT);
final WindowManager wm = (WindowManager) context.getSystemService(WINDOW_SERVICE);
wm.addView(inflatedViewWidget, params);

OnTouchListener:

inflatedViewWidget.setClickable(true);
inflatedViewWidget.setOnTouchListener(new View.OnTouchListener() {
       public boolean onTouch(View v, MotionEvent event) {

                        int action = event.getAction();
                        switch (action) {
                            case MotionEvent.ACTION_UP:
                                //how to forward event to Launcher app?
                                break;

答案1

得分: 0

AFAIK,在另一个应用程序中进行点击是不允许的,这将允许覆盖应用程序让您点击支付按钮,例如,您将需要 root 或调试能力来实现。

因此,关键是要调整窗口大小。
您的小部件的窗口大小和位置应始终与可点击区域匹配。
您可以使用 WindowManager 上的 updateViewLayout 方法 来实现这一点。

一个很好的示例可以在这里找到:
https://medium.com/@kevalpatel2106/create-chat-heads-like-facebook-messenger-32f7f1a62064

英文:

AFAIK, doing a click in another app is not allowed, that would allow overlay apps to make you click on a pay button for example, you would need root or debugging capabilities for that.

So the trick is to play with the window size.
Your widget's window size and location should always match the clickable area.
You can do this with the updateViewLayout method on the WindowManager.

A good example can be found here:
https://medium.com/@kevalpatel2106/create-chat-heads-like-facebook-messenger-32f7f1a62064

huangapple
  • 本文由 发表于 2020年8月27日 17:58:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/63613563.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定