我有一个应用程序,可以在驾驶时读取驱动程序的通知。我正在尝试解析各种通知。那些使用旧的' tickerText'工作正常,但像Skype这样的几个应用程序不支持。相反,在第一条消息之后的通知中的文本只是说了" 3条新消息"。不是很有帮助。我想要最后一个消息字符串。我写了这个:
public class Catcher extends NotificationListenerService {
File file;
String dir;
File save;
int count = 0;
@Override
public void onCreate() {
super.onCreate();
dir = Environment.getExternalStorageDirectory() + "/NotCatch";
save = new File(dir);
if(!save.exists()){
save.mkdirs();
}
//Toast.makeText(this, dir + " " + file.getName(), Toast.LENGTH_LONG).show();
}
@Override
public void onNotificationPosted(StatusBarNotification sbn) {
super.onNotificationPosted(sbn);
file = new File(save, "NotCatch" + count + ".txt");
count++;
String temp = "";
String[] lines;
Notification not = sbn.getNotification();
temp += "Category: " + not.category+ "\n";
temp+= "TickerText: " + not.tickerText + "\n";
temp += "Extras..."+ "\n";
Bundle bun = not.extras;
temp+= "BigText: " + bun.getString(Notification.EXTRA_BIG_TEXT)+ "\n";
temp+= "SummaryText: " + bun.getString(Notification.EXTRA_SUMMARY_TEXT)+ "\n";
temp+= "InfoText: " + bun.getString(Notification.EXTRA_INFO_TEXT)+ "\n";
temp+= "Text: " + bun.getString(Notification.EXTRA_TEXT)+ "\n";
temp+= "TextLines: " + bun.getString(Notification.EXTRA_TEXT_LINES)+ "\n";
temp+= "SubText: " + bun.getString(Notification.EXTRA_SUB_TEXT) + "\n";
temp+= "Title:" + bun.getString(Notification.EXTRA_TITLE) +"\n";
temp+= "Big Title:" + bun.getString(Notification.EXTRA_TITLE_BIG) +"\n";
/* lines = bun.getString(Notification.EXTRA_TEXT_LINES).split("\n");
temp += "Lines... \n" ;
int cnt = 0;
for (String line : lines) {
temp+= cnt + ": " + line + "\n";
cnt++;
}*/
Looper.prepare();
try {
FileOutputStream fos = new FileOutputStream(file);
fos.write(temp.getBytes());
fos.close();
Toast.makeText(this,"File written", Toast.LENGTH_LONG).show();
} catch (FileNotFoundException e) {
Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();
}
这正是我到目前为止进行测试的原因。我可以从通知中获取包,但它无法获取消息行的字符串数组。它在调试器中看起来像这样。 ' BUN'以下是' Bundle'来自'通知'。
我的问题是这个。如何获得以&#34开头的字符串数组;一些消息文本 - 4"?这是来自Android通知的捆绑包。
答案 0 :(得分:0)
由于您尝试检索的字符串包含在ArrayMap mMap中(根据调试器图像),并且由于Map的实现是可序列化的,因此您需要执行以下操作以首先检索该映射:< / p>
ArrayMap map = bun.getSerializableExtra("mMap");
然后,您应该能够获得所需的字符串数组(根据调试器图像是CharSequence对象的数组),具有以下内容:
CharSequence[] messages = (CharSequence[]) map.valueAt(1);
答案 1 :(得分:0)
我终于能够以这种方式获得字符串
CharSequence[] lines = bun.getCharSequenceArray(Notification.EXTRA_TEXT_LINES);
for(CharSequence line : lines){
temp+= "line: " + line.toString() + "\n";
}