我有我的Whatsapp应用程序的可访问性代码,但是此服务只有在我正常发送消息时才能工作。有谁知道我怎样才能让这个服务也为图像发送工作呢?
My WhatsappAccessibilityService
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);
}
发布于 2021-12-09 18:29:43
将这个类添加为AccessibiliyService,它将自动在我的whatsapp中发送图像。
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();
}
}
发布于 2021-07-21 04:58:36
/*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());
}
}
https://stackoverflow.com/questions/64975745
复制相似问题