如何从通知包中获取字符串数组?
How do I get the string array from a Notification bundle?
我有一个应用程序可以在开车时读取 driver 的通知。我正在尝试解析各种通知。那些使用旧 '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' 下面是 'Notification' 中的 'Bundle'。
我的问题是这样的。如何获取以 "some message text - 4" 开头的字符串数组?这是来自 Android 通知的捆绑包。
由于您尝试检索的字符串包含在 ArrayMap mMap 中(根据调试器图像),并且由于 Map 的实现是可序列化的,因此您需要执行以下操作来首先检索该映射:
ArrayMap map = bun.getSerializableExtra("mMap");
然后,您应该能够通过以下方式获得所需的字符串数组(根据调试器图像,它是一个 CharSequence 对象数组):
CharSequence[] messages = (CharSequence[]) map.valueAt(1);
我终于可以通过这种方式获取字符串
CharSequence[] lines = bun.getCharSequenceArray(Notification.EXTRA_TEXT_LINES);
for(CharSequence line : lines){
temp+= "line: " + line.toString() + "\n";
}
我有一个应用程序可以在开车时读取 driver 的通知。我正在尝试解析各种通知。那些使用旧 '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' 下面是 'Notification' 中的 'Bundle'。
我的问题是这样的。如何获取以 "some message text - 4" 开头的字符串数组?这是来自 Android 通知的捆绑包。
由于您尝试检索的字符串包含在 ArrayMap mMap 中(根据调试器图像),并且由于 Map 的实现是可序列化的,因此您需要执行以下操作来首先检索该映射:
ArrayMap map = bun.getSerializableExtra("mMap");
然后,您应该能够通过以下方式获得所需的字符串数组(根据调试器图像,它是一个 CharSequence 对象数组):
CharSequence[] messages = (CharSequence[]) map.valueAt(1);
我终于可以通过这种方式获取字符串
CharSequence[] lines = bun.getCharSequenceArray(Notification.EXTRA_TEXT_LINES);
for(CharSequence line : lines){
temp+= "line: " + line.toString() + "\n";
}