首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Whatsapp图像视图可访问性服务(Android )

Whatsapp图像视图可访问性服务(Android )
EN

Stack Overflow用户
提问于 2020-11-23 20:16:11
回答 2查看 1.5K关注 0票数 2

我有我的Whatsapp应用程序的可访问性代码,但是此服务只有在我正常发送消息时才能工作。有谁知道我怎样才能让这个服务也为图像发送工作呢?

My WhatsappAccessibilityService

代码语言:javascript
运行
复制
import android.accessibilityservice.AccessibilityService;
import android.provider.Settings;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;

import androidx.core.view.accessibility.AccessibilityNodeInfoCompat;

import java.util.List;

public  class WhatAppAccessibilityService extends AccessibilityService {

    @Override
    public void onAccessibilityEvent (AccessibilityEvent event) {
        if (getRootInActiveWindow () == null) {
            return;
        }

        AccessibilityNodeInfoCompat rootInActiveWindow = AccessibilityNodeInfoCompat.wrap (getRootInActiveWindow ());

        // Whatsapp Message EditText id
        List<AccessibilityNodeInfoCompat> messageNodeList = rootInActiveWindow.findAccessibilityNodeInfosByViewId ("com.whatsapp:id/entry");
        if (messageNodeList == null || messageNodeList.isEmpty ()) {
            return;
        }

        // check if the whatsapp message EditText field is filled with text and ending with your suffix (explanation above)
        AccessibilityNodeInfoCompat messageField = messageNodeList.get (0);
        if (messageField.getText () == null || messageField.getText ().length () == 0
                || !messageField.getText ().toString ().endsWith (getApplicationContext ().getString (R.string.whatsapp_suffix))) { // So your service doesn't process any message, but the ones ending your apps suffix
            return;
        }

        // Whatsapp send button id
        List<AccessibilityNodeInfoCompat> sendMessageNodeInfoList = rootInActiveWindow.findAccessibilityNodeInfosByViewId ("com.whatsapp:id/send");
        if (sendMessageNodeInfoList == null || sendMessageNodeInfoList.isEmpty ()) {
            return;
        }

        AccessibilityNodeInfoCompat sendMessageButton = sendMessageNodeInfoList.get (0);
        if (!sendMessageButton.isVisibleToUser ()) {
            return;
        }

        // Now fire a click on the send button
        sendMessageButton.performAction (AccessibilityNodeInfo.ACTION_CLICK);

        // Now go back to your app by clicking on the Android back button twice:
        // First one to leave the conversation screen
        // Second one to leave whatsapp
        try {
            Thread.sleep (500); // hack for certain devices in which the immediate back click is too fast to handle
            performGlobalAction (GLOBAL_ACTION_BACK);
            Thread.sleep (500);  // same hack as above
        } catch (InterruptedException ignored) {}
        performGlobalAction (GLOBAL_ACTION_BACK);
    }
EN

回答 2

Stack Overflow用户

发布于 2021-12-09 18:29:43

将这个类添加为AccessibiliyService,它将自动在我的whatsapp中发送图像。

代码语言:javascript
运行
复制
public class WhatsappAccessibilityService extends AccessibilityService{

    @Override
        if(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED == event.getEventType()) {
            Log.e("this", "ACC::onAccessibilityEvent : event=" + event);

            AccessibilityNodeInfo nodeInfo = event.getSource();
            Log.e("this", "ACC::onAccessibilityEvent : nodeInfo=" + nodeInfo);
            if (nodeInfo == null) {
                return;
            }


            //get whatsapp send message button node list
            List<AccessibilityNodeInfo> sendMessageNodeList = nodeInfo.findAccessibilityNodeInfosByViewId("com.whatsapp:id/send");
            if (sendMessageNodeList == null) {
                Log.v("hritik", "sendMessageNodeList is null");
            }
            for (AccessibilityNodeInfo node : sendMessageNodeList) {
                Log.e("this", "ACC::onAccessibilityEvent : send_button=" + node);
                node.performAction(AccessibilityNodeInfo.ACTION_CLICK);
                try {
                    Thread.sleep(1000); // hack for certain devices in which the immediate back click is too fast to handle
                    performGlobalAction(GLOBAL_ACTION_BACK);
                    Thread.sleep(1000);  // same hack as above
                    performGlobalAction(GLOBAL_ACTION_BACK);
                } catch (InterruptedException ignored) {
                }
                // performGlobalAction (GLOBAL_ACTION_BACK);
            }
        }

    }

    @Override
    public void onInterrupt() {
        Toast.makeText(getApplicationContext(), "not able to send", Toast.LENGTH_SHORT).show();
    
   }
}
票数 0
EN

Stack Overflow用户

发布于 2021-07-21 04:58:36

代码语言:javascript
运行
复制
/*Add this code in your AccessibiltyService*/    

if(PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getString("sendesc", "").equals("imagens")) {
    Log.e("test","4");
    List<AccessibilityNodeInfo> nodes = getRootInActiveWindow().findAccessibilityNodeInfosByViewId(PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getString("whatsz", "com.whatsapp.w4b") + ":id/send");
    if (nodes.size() > 0) {
    nodes.get(0).performAction(ACTION_CLICK);
    timer(2);
    Log.e("test","conversation_contact_name_A");
    List<AccessibilityNodeInfo> Whtsb = getRootInActiveWindow().findAccessibilityNodeInfosByViewId(PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getString("whatsz", "com.whatsapp.w4b") + ":id/conversation_contact_name");
    Log.e("test","conversation_contact_name_B");
    if (Whtsb.size() > 0) {
      if (Whtsb.get(0).getText().toString().contains("Contact_temp")) {
         Log.e("test","delete_contact_4");
    
     }
     }
    performGlobalAction(GLOBAL_ACTION_BACK);
    }
    } else {
       try {
     List<AccessibilityNodeInfo> Whtsb = getRootInActiveWindow().findAccessibilityNodeInfosByViewId(PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getString("whatsz", "com.whatsapp.w4b") + ":id/conversation_contact_name");
       if (Whtsb.size() > 0) {
    if (Whtsb.get(0).getText().toString().contains("Contact_temp")) {
    
    }
    }
    List<AccessibilityNodeInfo> nodes = getRootInActiveWindow().findAccessibilityNodeInfosByViewId(PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getString("whatsz", "com.whatsapp.w4b") + ":id/send");
     if (nodes.size() > 0) {
     nodes.get(0).performAction(ACTION_CLICK);
    }
    }catch (Exception e){
    e.printStackTrace();
    Log.e("TAG", "Error - "+e.toString());
    }
    }
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64975745

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档