如何从通知附加内容中读取whatsapp消息

时间:2015-11-03 19:14:31

标签: android mobile bitmap notifications whatsapp

嗨,我正在开发一个应用程序,我必须从whatsapp中读取图像。我可以用这行代码读取第一个图像作为位图:

Bitmap btm = (Bitmap) sbn.getNotification().extras.get(Notification.EXTRA_PICTURE); 

问题是,如果我收到多个图像,我会得到null。我该如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

这是NotificationListenerService

package com.etaure.callany.testwhatsappimage;

import android.accounts.AccountManager;
import android.app.Notification;
import android.content.Intent;
import android.graphics.Bitmap;
import android.service.notification.NotificationListenerService;
import android.service.notification.StatusBarNotification;
import android.support.annotation.Nullable;
import android.support.v4.content.LocalBroadcastManager;
import android.text.TextUtils;
import android.util.Log;

import org.json.JSONArray;

/**
 * Created by administrator on 03/11/2015.
 */
public class MyNotificationListner extends NotificationListenerService {

    static int i = 0;

    @Override
    public void onNotificationPosted(StatusBarNotification sbn) {

        if (sbn.getPackageName().equals("com.whatsapp")) {

            Bitmap btm = (Bitmap) sbn.getNotification().extras.get(Notification.EXTRA_PICTURE);

        }
    }

    @Override
    public void onNotificationRemoved(StatusBarNotification sbn) {
        Log.w("Test", "NotificationRemoved");
    }
}

答案 1 :(得分:0)

这是一个代码,用于解析你的logcat所有base64字符串上的whatsapp图像和节目(如果你愿意)!享受

`

           if (extras.containsKey(Notification.EXTRA_PICTURE)) {
            bmp = (Bitmap) extras.get(Notification.EXTRA_PICTURE);
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            bmp.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
            byteArrayS = byteArrayOutputStream .toByteArray();
            encoded = Base64.encodeToString(byteArrayS, Base64.DEFAULT);

            final int LOGCAT_MAX_LENGTH = 3950;
            if (BuildConfig.DEBUG) {
                while (encoded.length() > LOGCAT_MAX_LENGTH) {
                    int substringIndex = encoded.lastIndexOf(",", LOGCAT_MAX_LENGTH);
                    if (substringIndex == -1)
                        substringIndex = LOGCAT_MAX_LENGTH;
                    Log.d("encode", encoded.substring(0, substringIndex));
                    encoded = encoded.substring(substringIndex).trim();
                }
                Log.d("encode", encoded);
            }

        }

`