Olá, boa tarde! Desculpe pela mudança de contextos, porém me identifiquei com a questão de armazenamento de textos dentro de uma lista! Sou desenvolvedor e estou fazendo um aplicativo onde eu redireciono todas as notificações que chegam no meu celular para uma ListView que está no meu app. Estou tendo problemas quando recebo a primeira mensagem do whatsapp, sendo que quando essa chega, no logcat, me retorna nulo. Alguma sugestão de como eu poderia armazenar todas as mensagens uma por uma dentro dessa lista? Código abaixo representa a classe que eu uso para "ouvir" essas notificações e armazená-las dentro do intent, para assim redirecioná-las, de fato, dentro da minha main, a notificação com o texto e a imagem da pessoa. Obrigado pela atenção!
package com.example.biels.testeprojeto;
import android.app.Notification;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.service.notification.NotificationListenerService;
import android.service.notification.StatusBarNotification;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import java.io.ByteArrayOutputStream;
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();
}
Bundle extras = sbn.getNotification().extras;
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("Package",pack);
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);
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");
}
}