如何使用Android的辅助功能自动单击并关闭Whats App弹出通知,然后返回主应用程序

问题描述 投票:0回答:1

这是我必须完成的大学项目的一部分。在这个项目应用程序中,我通过以下 java 代码使用 android 的辅助功能自动发送应用程序消息。

    numberlist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

        try {
            Toast.makeText(getBaseContext(), (String) numberlist.getItemAtPosition(i), Toast.LENGTH_SHORT).show();
            txtnumber.setText(numberlist.getItemAtPosition(i).toString());
            String suffix;
            suffix = "Thanks";

            PackageManager packageManager = MainActivity.this.getPackageManager();
            Intent saif = new Intent(Intent.ACTION_VIEW);
            String message;
            message = txtmessage.getText().toString() + suffix.toString();
            String phone = txtnumber.getText().toString();
            String url = "https://api.whatsapp.com/send?phone=" + phone + "&text=" + message;
            saif.setPackage("com.whatsapp");
            saif.setData(Uri.parse(url));
            if (saif.resolveActivity(packageManager) != null) {
                MainActivity.this.startActivityForResult(saif, 1);
            }
        } catch (Exception e) {
            Log.e("ERROR WHATSAPP",e.toString());
            Toast.makeText(getApplicationContext(),
                    "Contact not found",
                    Toast.LENGTH_LONG).show();

        }
    }

});

如果用户的手机号码是在 WhatsApp 应用程序中创建的,则消息发送会很容易。如果某个手机号码未创建 WhatsApp,则会显示以下消息。

我使用了以下辅助功能节点信息....

    List<AccessibilityNodeInfoCompat> sendMessageNodeInfoList = rootInActiveWindow.findAccessibilityNodeInfosByViewId("com.whatsapp:id/send");
    String actname = event.getClassName().toString();
    switch (actname) {
        case "com.whatsapp.Conversation":
            sendMessageNodeInfoList = rootInActiveWindow.findAccessibilityNodeInfosByViewId("com.whatsapp:id/send");
            if (sendMessageNodeInfoList.size() > 0) {
                sendMessageNodeInfoList.get(0).performAction(ACTION_CLICK);
            }
            break;
        case "com.whatsapp.HomeActivity":
            sendNext();
            break;
        case "com.whatsapp.contact.picker.ContactPicker":
            Toast.makeText(this, "Unable to find contacts in your list! Skipping!!!", Toast.LENGTH_SHORT).show();
            performGlobalAction(GLOBAL_ACTION_BACK);
            break;
    }
    assert sendMessageNodeInfoList != null;
    AccessibilityNodeInfoCompat sendMessageButton = sendMessageNodeInfoList.get(0);
    if (!sendMessageButton.isVisibleToUser()) {

        return;
    }
    sendMessageButton.performAction(ACTION_CLICK);

我的问题是,我们如何使用 Android 的辅助功能通过自动单击“确定”或“取消”按钮来关闭此通知?我希望当 Whats App 消息发送到用户的手机号码时,应首先检查他的手机号码是否在 Whats App 上可用。如果在该手机号码上创建了 Whats App,则应向他发送一条消息;如果在他的手机号码上未创建 Whats App,则自动单击此弹出通知将其关闭并返回主屏幕。这是我大学必须完成的最后一个项目,请帮助我完成它。谢谢

java android accessibility whatsapp popupwindow
1个回答
0
投票

我已经用以下代码解决了这个问题。如果有人在这方面需要我的帮助,可以联系我。感谢阿拉帮助了我,我通过一次又一次的尝试解决了这个问题。

    if (accessibilityEvent.toString().contains("ClassName: com.whatsapp.contact.picker.ContactPicker")) {
                    Thread.sleep(100);
                    MainActivity.V0++;
                    performGlobalAction(GLOBAL_ACTION_BACK);
                    Intent intent2 = new Intent("NextNumber");
                    intent2.setPackage(getPackageName());
                    getApplicationContext().sendBroadcast(intent2);
                    return;
                }
                AccessibilityNodeInfo source = accessibilityEvent.getSource();
                if (source != null) {
                    List<AccessibilityNodeInfo> findAccessibilityNodeInfosByViewId = source.findAccessibilityNodeInfosByViewId(MainActivity.R0 + ":id/send");
                    if (findAccessibilityNodeInfosByViewId == null) {
                        return;
                    }
                    if (!findAccessibilityNodeInfosByViewId.isEmpty()) {
                        AccessibilityNodeInfo accessibilityNodeInfo = findAccessibilityNodeInfosByViewId.get(0);
                        if (accessibilityNodeInfo.isVisibleToUser()) {
                            accessibilityNodeInfo.performAction(ACTION_CLICK);
                            Thread.sleep(900);
                            MainActivity.V0++;
                            performGlobalAction(GLOBAL_ACTION_BACK);
                            intent = new Intent("NextNumber");
                            packageName = getPackageName();
                        } else {
                            return;
                        }
                    } else {
                        return;
                    }
                } else {
                    return;
                }
            } else if (getRootInActiveWindow() != null) {
                AccessibilityNodeInfo rootInActiveWindow = getRootInActiveWindow();
                 i iVar = new i(rootInActiveWindow, 0);
                AccessibilityNodeInfo child = rootInActiveWindow.getChild(0);
                String iVar2 = (child != null ? new i(child, 0) : null).toString();
                if (!iVar2.contains("text: The phone number +" +MainActivity.txtnumber.getText().toString()+"isn't on WhatsApp ")) {
                    if (!iVar2.contains("text: The phone number +" +MainActivity.txtnumber.getText().toString()+"isn't on WhatsApp")) {
                        ArrayList d7 = iVar.d(MainActivity.R0 + ":id/entry");
                        if (!d7.isEmpty()) {
                            i iVar3 = (i) d7.get(0);
                            if (!(iVar3.f() == null && iVar3.f().length() == 0) && iVar3.f().toString().endsWith(getApplicationContext().getString(R.string.whatsapp_suffix))) {
                                ArrayList d8 = iVar.d(MainActivity.R0 + ":id/send");
                                if (!d8.isEmpty()) {
                                    i iVar4 = (i) d8.get(0);
                                    if (iVar4.f2707a.isVisibleToUser()) {
                                        MainActivity.V0++;
                                        iVar4.f2707a.performAction(ACTION_CLICK);
                                        Thread.sleep(700);
                                        performGlobalAction(GLOBAL_ACTION_BACK);
                                        Thread.sleep(700);
                                        performGlobalAction(GLOBAL_ACTION_BACK);
                                        intent = new Intent("NextNumber");
                                        packageName = getPackageName();
                                    } else {
                                        return;
                                    }
                                } else {
                                    return;
                                }
                            } else {
                                return;
                            }
                        } else {
                            return;
                        }
                    }
                }
                MainActivity.V0++;
                performGlobalAction(GLOBAL_ACTION_BACK);
                Thread.sleep(400);
                performGlobalAction(GLOBAL_ACTION_BACK);
                Intent intent3 = new Intent("NextNumber");
                intent3.setPackage(getPackageName());
                getApplicationContext().sendBroadcast(intent3);
                return;
© www.soinside.com 2019 - 2024. All rights reserved.