gpt4 book ai didi

android - 在后台以编程方式发送 WhatsApp 消息,无需打开 WhatsApp 应用程序

转载 作者:行者123 更新时间:2023-12-04 23:39:13 28 4
gpt4 key购买 nike

我为类似的问题找到了很多线索,但在任何地方我都找不到我要找的东西。

给定联系人号码和短信,我想以编程方式向该联系人发送 WhatsApp 消息,而无需打开应用程序或选择联系人。

到目前为止,我已经编写了以下代码:

private static void sendTextUsingWhatsapp(Context context, Pair<S, S> contact, String Text) {    
Intent sendIntent = new Intent("android.intent.action.MAIN");
sendIntent.setComponent(new ComponentName("com.whatsapp","com.whatsapp.Conversation"));
sendIntent.putExtra("jid", PhoneNumberUtils.stripSeparators(countryCode +
contact.second)+"@s.whatsapp.net");
sendIntent.putExtra(Intent.EXTRA_TEXT, text);
sendIntent.setType("text/plain");
sendIntent.setAction(Intent.ACTION_SEND);
context.startActivity(sendIntent);
}

通过这段代码,我进入了 WhatsApp 联系人窗口。

This SO question声明不支持这种功能。但是,Google Now(“OK Google,发送消息”)如何能够在后台发送 WhatsApp 消息?

最佳答案

您无法在后台发送 WhatsApp 消息。您可以使用 android 无障碍服务来自动化它。
我的 AndroidManifest.xml

<service android:name=".helper.MyAccessibilityService"
android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE"
android:label="@string/app_name"
android:exported="true">
<meta-data
android:name="android.accessibilityservice"
android:resource="@xml/config_accessibility_service" />
<intent-filter>
<action android:name="android.accessibilityservice.AccessibilityService" />
</intent-filter>
</service>
config_accessibility_service.xml
<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
android:description="@string/app_name"
android:accessibilityEventTypes="typeAllMask"
android:accessibilityFlags="flagDefault"
android:accessibilityFeedbackType="feedbackSpoken"
android:notificationTimeout="100"
android:canRetrieveWindowContent="true"
android:settingsActivity="com.example.android.accessibility.ServiceSettingsActivity"/>
MyAccessibilityService.java
import android.accessibilityservice.AccessibilityService;
import android.accessibilityservice.AccessibilityServiceInfo;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.PowerManager;
import android.util.Log;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;

import androidx.core.view.accessibility.AccessibilityNodeInfoCompat;

import java.util.List;

public class MyAccessibilityService extends AccessibilityService {

private static final String TAG = "MyAccessibilityService";

@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
String packageName = event.getPackageName().toString();
if (packageName.equals("com.whatsapp")){
PackageManager packageManager = this.getPackageManager();
try {
ApplicationInfo info = packageManager.getApplicationInfo(packageName,0);
String name = packageManager.getApplicationLabel(info).toString();
try {
AccessibilityNodeInfoCompat rootNodeInfo = AccessibilityNodeInfoCompat.wrap(getRootInActiveWindow());
List<AccessibilityNodeInfoCompat> sendMessageNodeList = rootNodeInfo.findAccessibilityNodeInfosByViewId("com.whatsapp:id/send");
if (sendMessageNodeList == null || sendMessageNodeList.isEmpty()){
return;
}
AccessibilityNodeInfoCompat sendMessage = sendMessageNodeList.get(0);
if (!sendMessage.isVisibleToUser()){
return;
}
sendMessage.performAction(AccessibilityNodeInfo.ACTION_CLICK);

try {
Thread.sleep(2000);
performGlobalAction(GLOBAL_ACTION_BACK);
Thread.sleep(2000);
}catch (Exception e){
Log.e("onAccessibilityEvent", "onAccessibilityEvent: ",e );
}
}catch (Exception e){
e.printStackTrace();
}
Log.e(TAG, "onAccessibilityEvent: "+name );
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
}
}

@Override
public void onInterrupt() {

}

@Override
protected void onServiceConnected() {
super.onServiceConnected();
AccessibilityServiceInfo info = new AccessibilityServiceInfo();
info.eventTypes = AccessibilityEvent.TYPE_VIEW_CLICKED |
AccessibilityEvent.TYPE_VIEW_FOCUSED;
info.packageNames = new String[]
{"com.whatsapp"};
info.feedbackType = AccessibilityServiceInfo.FEEDBACK_SPOKEN;
this.setServiceInfo(info);
Log.d(TAG, "Accessibility service connected");
}
}
AccessibilityServiceManager.java
public class AccessibilityServiceManager {
Context context;

public AccessibilityServiceManager(Context context) {
this.context = context;
}

public boolean hasAccessibilityServicePermission(Class<? extends AccessibilityService> clazz) {
int accessibilityEnabled = 0;
final String service = context.getPackageName() + "/" + clazz.getCanonicalName();
try {
accessibilityEnabled = Settings.Secure.getInt(context.getApplicationContext().getContentResolver(), Settings.Secure.ACCESSIBILITY_ENABLED);
} catch (Settings.SettingNotFoundException ignored) {
}
TextUtils.SimpleStringSplitter colonSplitter = new TextUtils.SimpleStringSplitter(':');
if (accessibilityEnabled == 1) {
String settingValue = Settings.Secure.getString(context.getApplicationContext().getContentResolver(), Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
if (settingValue != null) {
colonSplitter.setString(settingValue);
while (colonSplitter.hasNext()) {
String accessibilityService = colonSplitter.next();

if (accessibilityService.equalsIgnoreCase(service)) {
return true;
}
}
}
}

return false;
}
public void requestUserForAccessibilityService(Activity activity){
new AlertDialog.Builder(context)
.setTitle("Permission needed")
.setMessage("You have to enable Accessibility service to use this feature")
.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
})
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Intent intent = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS);
activity.startActivity(intent);
}
})
.show();

}
}
你可以在 MainActivity.java 中调用它
AccessibilityServiceManager serviceManager = new AccessibilityServiceManager(this);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (serviceManager.hasAccessibilityServicePermission(MyAccessibilityService.class)){
String message = "Your message",to = "91XXXXXXXXXX";
startActivity(
new Intent(Intent.ACTION_VIEW,
Uri.parse(
String.format("https://api.whatsapp.com/send?phone=%s&text=%s", to, message)
)
).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
);
}else{
serviceManager.requestUserForAccessibilityService(MainActivity.this);
}
}
});
这对我有用。

关于android - 在后台以编程方式发送 WhatsApp 消息,无需打开 WhatsApp 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42054588/

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