Send WhatsApp message in background or send messag

2020-03-04 02:59发布

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

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?

Thanks!

3条回答
▲ chillily
2楼-- · 2020-03-04 03:08

I don't believe there exists any such way, else others would have been using it already as it is one of the most desired functionality. SMS and WhatsApp are way different. SMS are build deep into the system.

However if you wish to automate texts through WhatsAap, it can be done through other methods that don't really involve coding. Sorry I am not allowed to comment n question yet, so I have written this as an answer.

查看更多
smile是对你的礼貌
3楼-- · 2020-03-04 03:10

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.

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

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

                        }
                }
}

and the parsing method.

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.

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>

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);
查看更多
爷的心禁止访问
4楼-- · 2020-03-04 03:17

I believe this is possible as the 3rd party Android Wear app, Quick for Wear let's you send Whatsapp, Messenger, Gmail, etc. messages directly from the watch.

查看更多
登录 后发表回答