在后台发送WhatsApp的消息或发送消息,并关闭该应用程序在Android中 [英] Send WhatsApp message in background or send message and close the app in android

查看:389
本文介绍了在后台发送WhatsApp的消息或发送消息,并关闭该应用程序在Android中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

时可以发送WhatsApp的消息,而无需打开应用程序,在后台发送就像使用发送短信:

Is it possible to send whatsApp message without opening the app, to be sent in the background like sending SMS using:

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

如果又如何?在code,我试图打开APP(意图):

if so how? The code that I tried opens the APP (WITH 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"));

或者是有可能打开应用程序发送消息给定的地址,并关闭它?

Or is it possible open the app send message to given address and close it?

谢谢!

推荐答案

没有,这是不可能的,因为WhatsApp的不报价ContentProviders并不会在未来。

No, it's not possible since Whatsapp dont offer ContentProviders and wont in future.

有可能重新拆卸和实施是基于闲聊协议

It is possible re-disassembling and implementing the protocol which is based on jabber.

您将需要处理的握手等。

You will need to handle the handshake etc.

不管怎么说,这是不可能的,甚至不是用root如果你不想组装整个网络的东西。

Anyway, it's not possible, not even with root if you dont want to assemble the whole networking stuff.

由于您的第二个问题(捕获WhatsApp的消息和关闭应用程序为例):

Because of your second Question (Capturing Whatsapp Messages and closing the app for example):

您将需要一个无障碍环境服务,捕获在给定的包所有传入的事件。例如,在这里:

You will need an Accesibility Service which capture all incoming events on the given package. EXample here:

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));
        }
}

现在,您可以分析一个消息的原始消息,并做你想做的。

You can now parse on the raw message for a message and do whatever you want.

不要忘了注册你的清单中accesibilityService并让用户启用它。
         

Do not forget to register the accesibilityService in your manifest and let the user enable it.

    <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文件应包含要启用您accesibilityservice什么的。

and your xml/accesibilityservice.xml file should contain whatever you want to enable for your accesibilityservice.

<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" />

和别忘了让用户激活它。你可以通过调用直接获得用户设置

And do not forget to let the user activate it. You can get the user directly to the setting by calling

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

这篇关于在后台发送WhatsApp的消息或发送消息,并关闭该应用程序在Android中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆