listview即将发布的通知文本和来自whatsapp的图片

时间:2017-08-04 15:45:56

标签: android image listview text notifications

我是Android上的新开发者,我尝试向ListView提供通知(文字和图片)。我已经完成了文字,但我没有完成即将发布的图片通知。 我尝试过使用位图和字节数组,但还没有完成。

这是我的代码

public class NotificationService extends NotificationListenerService {
    Context context;

    @Override

    public void onCreate() {

        super.onCreate();
        context = getApplicationContext();

    }
    @Override

    public void onNotificationPosted(StatusBarNotification sbn) {
        String pack = sbn.getPackageName();
        String ticker ="";
        if(sbn.getNotification().tickerText !=null) {
            ticker = sbn.getNotification().tickerText.toString();
        }
        Bitmap bmp = null;
        Bundle extras = sbn.getNotification().extras;
        if (extras.containsKey(Notification.EXTRA_PICTURE)) {

            bmp = (Bitmap) extras.get(Notification.EXTRA_PICTURE);


        }
        String title = extras.getString("android.title");
        String text = null;
        if (text == null) {
            if (extras.get("android.textLines") != null) {
                CharSequence[] charText = (CharSequence[]) extras
                        .get("android.textLines");
                if (charText.length > 0) {
                    text = charText[charText.length - 1].toString();
                }
            }
        }
        int id1 = extras.getInt(Notification.EXTRA_SMALL_ICON);

        Bitmap id = sbn.getNotification().largeIcon;



        Log.i("Ticker",ticker);
        Log.i("Title",title);
        Log.i("Text",text);



        Intent msgrcv = new Intent("Msg");
        msgrcv.putExtra("package", pack);
        msgrcv.putExtra("ticker", ticker);
        msgrcv.putExtra("title", title);
        msgrcv.putExtra("text", text);
        msgrcv.putExtra("byteArray",bmp);




        if(id != null) {
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            id.compress(Bitmap.CompressFormat.PNG, 100, stream);
            byte [] byteArray = stream.toByteArray();
            msgrcv.putExtra("icon",byteArray);
        }
        LocalBroadcastManager.getInstance(context).sendBroadcast(msgrcv);


    }

    @Override

    public void onNotificationRemoved(StatusBarNotification sbn) {
        Log.i("Msg","Notification Removed");

    }
}

感谢您的帮助!

0 个答案:

没有答案