Java 向Whatsapp用户发送图像
我试图通过Stackoverflow中“user1079425”指示的可访问性服务按下send按钮,将图像发送到一定数量的Whatsapp 以下是我使用的代码:Java 向Whatsapp用户发送图像,java,android,whatsapp,Java,Android,Whatsapp,我试图通过Stackoverflow中“user1079425”指示的可访问性服务按下send按钮,将图像发送到一定数量的Whatsapp 以下是我使用的代码: private void sendImageWhatsapp (String telephone){ String toNumber, tel, message; String mystring = "\u0007"; // adjust tel.number tel = telephone.repl
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() {
}
}
private void sendImageWhatsapp(字符串电话){
字符串toNumber、tel、message;
字符串mystring=“\u0007”;
//调整电话号码
电话=电话.replaceAll(“^0-9+]”,“”);
if(电话子串(0,1).equalsIgnoreCase(“+”)){
电话=电话子串(1);
}
否则{
if(电话子串(0,2).equalsIgnoreCase(“00”)){
电话=电话子串(2);
}
否则{
tel=context.getApplicationContext().getResources().getString(R.string.prefix_code)+tel;
}
}
//字符串toNumber=“+91 98765 43210”//包含空格。
//toNumber=toNumber.replace(“+”,”).replace(“,”);
toNumber=tel;//E164格式,不带“+”号
message=imageDidascalia.getText().toString()+mystring;
Intent sendIntent=newintent(“android.Intent.action.MAIN”);
//sendIntent.putExtra(Intent.EXTRA_流,Uri.fromFile(imageFile));
sendIntent.putExtra(Intent.EXTRA_流,imageUri);
putExtra(“jid”,toNumber+“@s.whatsapp.net”);
sendIntent.putExtra(Intent.EXTRA_文本、消息);
sendIntent.setAction(Intent.ACTION\u SEND);
sendIntent.setPackage(“com.whatsapp”);
setType(“image/png”);
背景。起始触觉(sendIntent);
}
公共类WhatsappAccessibilityService扩展了AccessibilityService{
私有整数指数=0;
@凌驾
AccessibilityEvent上的公共无效(AccessibilityEvent事件){
if(getRootInActiveWindow()==null){
返回;
}
AccessibilityNodeInfoCompat rootInActiveWindow=AccessibilityNodeInfoCompat.wrap(getRootInActiveWindow());
//Whatsapp消息编辑文本id
List messageNodeList=rootInActiveWindow.findAccessibilityNodeInfosByViewId(“com.whatsapp:id/entry”);
if(messageNodeList==null | | messageNodeList.isEmpty()){
//Whatsapp图像编辑文本id
指数=1;
messageNodeList=rootInActiveWindow.findAccessibilityNodeInfosByViewId(“com.whatsapp:id/pickfiletype_gallery”);
if(messageNodeList==null | | messageNodeList.isEmpty()){
返回;
}`在这里输入代码`
//返回;
}
//检查whatsapp message EditText字段是否用文本填充,并以您的后缀结尾(以上说明)
AccessibilityNodeInfoCompat messageField=messageNodeList.get(索引);
if(messageField.getText()==null | | messageField.getText().length()==0
||!messageField.getText().toString().endsWith(“\u0007”){//因此,您的服务不会处理任何消息,但会处理以应用程序结尾的消息
//| | |!messageField.getText().toString().endsWith(getApplicationContext().getString(R.string.whatsapp_后缀)){//因此,您的服务不会处理任何消息,但会处理以应用后缀结尾的消息
返回;
}
//Whatsapp发送按钮id
List sendMessageNodeInfo=rootInActiveWindow.findAccessibilityNodeInfosByViewId(“com.whatsapp:id/send”);
if(sendMessageNodeInfo==null | | sendMessageNodeInfo.isEmpty()){
返回;
}
AccessibilityNodeInfoCompat sendMessageButton=sendMessageNodeInfoList.get(0);
如果(!sendMessageButton.isVisibleToUser()){
返回;
}
//现在点击发送按钮
sendMessageButton.performAction(AccessibilityNodeInfo.ACTION\u单击);
//现在,单击Android后退按钮两次,返回应用程序:
//第一个离开对话屏幕的人
//第二个离开whatsapp的人
试一试{
Thread.sleep(500);//针对某些设备的黑客攻击,在这些设备中,立即返回的点击速度太快,无法处理
执行全局行动(全局行动返回);
Thread.sleep(500);//与上面的方法相同
}catch(InterruptedException忽略){}
执行全局行动(全局行动返回);
}
@凌驾
在中断时的公共无效(){
}
}
此代码适用于消息,但不适用于图像。我也遇到了同样的问题。在花了很长时间之后,我解决了这个问题。如果你想发送一个没有用户交互的图像,只需删除你正在应用于WhatsApp编辑文本的代码和条件。我将直接跳到按钮您的图像将自动发送到您通过的号码。 ` 覆盖AccessibilityEvent(事件:AccessibilityEvent){ if(rootInActiveWindow==null){ 返回 } val rootInActiveWindow=AccessibilityNodeInfoCompat.wrap(rootInActiveWindow) ` 如果您将图像与文本一起传递,编辑文本将返回null,因此我们对编辑文本的代码进行注释。请仔细阅读我如何对Whatsapp的编辑文本的代码进行注释,以便它直接执行按钮单击
// 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)
}