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

时间:2014-06-18 06:01:21

标签: android whatsapp

是否可以在不打开应用程序的情况下发送whatsApp消息,在后台发送,如使用以下方式发送短信:

smsManager.sendTextMessage("+12546304580", null, "Test Message", null, null);
如果是这样怎么样?我尝试的代码打开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"));

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

谢谢!

3 个答案:

答案 0 :(得分:5)

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

可以重新拆解和实施基于jabber的协议。

您需要处理握手等。

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

由于您的第二个问题(捕获Whatsapp消息并关闭应用程序):

您需要一个Accesibility Service来捕获给定包上的所有传入事件。例如:

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

您现在可以解析邮件的原始邮件并执行任何操作。

不要忘记在清单中注册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);

答案 1 :(得分:0)

我不相信存在任何此类方式,否则其他人已经使用它,因为它是最理想的功能之一。 SMS和WhatsApp有所不同。 SMS深入到系统中。

但是,如果您希望通过WhatsAap自动化文本,可以通过其他不真正涉及编码的方法来完成。 对不起,我暂时不能发表评论,所以我写了这个答案。

答案 2 :(得分:0)

我认为这可以作为第三方Android Wear应用程序,Quick for Wear让您直接从手表发送Whatsapp,Messenger,Gmail等消息。

相关问题