gpt4 book ai didi

android - 在后台发送 WhatsApp 消息或发送消息并在 android 中关闭应用程序

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:48:07 27 4
gpt4 key购买 nike

是否可以在不打开应用程序的情况下发送 whatsApp 消息,像发送短信一样在后台发送:

smsManager.sendTextMessage("+12546304580", null, "Test Message", null, null);

如果是这样怎么办?我尝试打开应用程序的代码(有 Intent ):

    Intent waIntent = new Intent(Intent.ACTION_SEND);
waIntent.setType("text/plain");
String text = "Test Message";
waIntent.setPackage("com.whatsapp");
waIntent.putExtra(Intent.EXTRA_TEXT, text);//
startActivity(Intent.createChooser(waIntent, "Share with"));

或者是否可以打开应用程序发送消息到给定地址并关闭它?

谢谢!

最佳答案

不,这是不可能的,因为 Whatsapp 不提供 ContentProvider,将来也不会。

基于jabber的协议(protocol)可以重新拆解实现。

您将需要处理握手等。

无论如何,这是不可能的,如果你不想组装整个网络的东西,即使是 root 也是不可能的。

因为你的第二个问题(例如捕获 Whatsapp 消息并关闭应用程序):

您将需要一个可访问性服务来捕获给定包上的所有传入事件。这里的例子:

public static final String FACEBOOK_PACKAGE_NAME = "com.facebook.orca";

public class DefaultAccessibility extends AccessibilityService {
@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
final int eventType = event.getEventType();
try {
if (Build.VERSION.SDK_INT >= 16) {
switch (eventType) {
case AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED:
Notification notification = (Notification) event.getParcelableData();
if (event.getPackageName().equals(WHATSAPP_PACKAGE_NAME)) {
RemoteViews views = notification.bigContentView;
Class<?> secretClass = views.getClass();
ArrayList<String> raw = new ArrayList<String>();
Field outerFields[] = secretClass.getDeclaredFields();
for (Field outerField : outerFields) {
if (outerField.getName().equals("mActions")) {
outerField.setAccessible(true);
ArrayList<Object> actions = null;
try {
actions = (ArrayList<Object>) outerField.get(views);
for (Object action : actions) {
Field innerFields[] = action.getClass()getDeclaredFields();

Object value = null;
Integer type = null;
for (Field field : innerFields) {
try {
field.setAccessible(true);
if (field.getName().equals("type")) {
type = field.getInt(action);
} else if (field.getName().equals("value")) {
value = field.get(action);
}
} catch (IllegalArgumentException e) {
} catch (IllegalAccessException e) {
}
}

if (type != null && type == 10 && value != null) {
raw.add(value.toString());
}
}
} catch (IllegalArgumentException e1) {
} catch (IllegalAccessException e1) {
}
}
parseWhatsappRawMessages(raw);

}
}
}

和解析方法。

private void parseWhatsappRawMessages(ArrayList<String> raw) {

int count = raw.size();
if (count > 2) {
Log.d(TAG, "RAW TITLE: " + raw.get(0));
Log.d(TAG, "RAW MESSAGE: " + raw.get(1));
}
}

您现在可以在原始消息上解析消息并执行任何您想做的事情。

不要忘记在 list 中注册 accesibilityService 并让用户启用它。

    <service
android:name="com.app.DefaultAccessibility"
android:enabled="true"
android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE">
<intent-filter>
<action android:name="android.accessibilityservice.AccessibilityService"></action>
</intent-filter>
<meta-data
android:name="android.accessibilityservice"
android:resource="@xml/accessibilityservice"/>
</service>

并且您的 xml/accesibilityservice.xml 文件应该包含您想要为您的可访问性服务启用的任何内容。

<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
android:accessibilityEventTypes="typeAllMask"
android:accessibilityFeedbackType="feedbackAllMask"
android:notificationTimeout="100"
android:description="@string/accessibility_service_description" />

并且不要忘记让用户激活它。您可以通过调用让用户直接进入设置

Intent intent = new Intent(android.provider.Settings.ACTION_ACCESSIBILITY_SETTINGS);
startActivityForResult(intent, 1337);

关于android - 在后台发送 WhatsApp 消息或发送消息并在 android 中关闭应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24278082/

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