gpt4 book ai didi

java - 向 Whatsapp 用户发送图像

转载 作者:行者123 更新时间:2023-12-02 01:05:13 24 4
gpt4 key购买 nike

我正在尝试通过 Stackoverflow Send message via whatsapp programmatically 中“user1079425”指示的辅助服务按“发送”按钮,将图像发送到一定数量的 Whatsapp。 .

这是我正在使用的代码:

private void sendImageWhatsapp (String telephone){

String toNumber, tel, message;
String mystring = "\u0007";

// adjust tel.number
tel = telephone.replaceAll("[^0-9+]","");
if (tel.substring(0,1).equalsIgnoreCase("+")){
tel = tel.substring(1);
}
else{
if (tel.substring(0,2).equalsIgnoreCase("00")){
tel = tel.substring(2);
}
else{
tel = context.getApplicationContext().getResources().getString(R.string.prefix_code) + tel;
}
}

//String toNumber = "+91 98765 43210"; // contains spaces.
//toNumber = toNumber.replace("+", "").replace(" ", "");

toNumber = tel; // E164 format without '+' sign
message = imageDidascalia.getText().toString() + mystring;

Intent sendIntent = new Intent("android.intent.action.MAIN");
//sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(imageFile));
sendIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
sendIntent.putExtra("jid", toNumber + "@s.whatsapp.net");
sendIntent.putExtra(Intent.EXTRA_TEXT, message);
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.setPackage("com.whatsapp");
sendIntent.setType("image/png");
context.startActivity(sendIntent);
}

public class WhatsappAccessibilityService extends AccessibilityService {

private int index = 0;

@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 ()) {
// Whatsapp Image EditText id
index=1;
messageNodeList = rootInActiveWindow.findAccessibilityNodeInfosByViewId ("com.whatsapp:id/pickfiletype_gallery");
if (messageNodeList == null || messageNodeList.isEmpty ()) {
return;
}`enter code here`

//return;
}

// check if the whatsapp message EditText field is filled with text and ending with your suffix (explanation above)
AccessibilityNodeInfoCompat messageField = messageNodeList.get (index);
if (messageField.getText () == null || messageField.getText ().length () == 0
|| !messageField.getText ().toString ().endsWith ("\u0007")) { // So your service doesn't process any message, but the ones ending your apps suffix
// || !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);
}

@Override
public void onInterrupt() {
}
}

此代码适用于消息,但不适用于图像。

最佳答案

我也遇到了同样的问题。我花了很长时间解决了这个问题。如果您想在没有用户交互的情况下发送图像,您只需删除应用于 WhatsApp 编辑文本的代码和条件即可。我会直接跳转到按钮代码,您的图像将自动发送到您输入的号码。`覆盖 fun onAccessibilityEvent(事件: AccessibilityEvent) { if (rootInActiveWindow == null) { 返回 } val rootInActiveWindow = AccessibilityNodeInfoCompat.wrap(rootInActiveWindow)

    // Whatsapp Message EditText id
/*val 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)
val messageField = messageNodeList[0]
if (messageField.text == null || messageField.text.length == 0 || !messageField.text.toString().endsWith("abcd")) { // So your service doesn't process any message, but the ones ending your apps suffix
return
}*/

// Whatsapp send button id
val sendMessageNodeInfoList = rootInActiveWindow.findAccessibilityNodeInfosByViewId("com.whatsapp:id/send")
if (sendMessageNodeInfoList == null || sendMessageNodeInfoList.isEmpty()) {
return
}
val sendMessageButton = sendMessageNodeInfoList[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(2800) // hack for certain devices in which the immediate back click is too fast to handle
performGlobalAction(GLOBAL_ACTION_BACK)
Thread.sleep(2800) // same hack as above
} catch (ignored: InterruptedException) {
}
performGlobalAction(GLOBAL_ACTION_BACK)
}

`如果将图像与文本一起传递,则编辑文本将返回 null,因此我们注释编辑文本的代码。请仔细阅读我如何注释 Whatsapp 编辑文本的代码,以便它直接执行按钮单击。

关于java - 向 Whatsapp 用户发送图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57738868/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com