2016-06-19 17 views
0

運転中にドライバに通知を読みとるアプリがあります。私は様々な通知を解析しようとしています。古い '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(); 
    } 

これは、これまで私がこれまでテストしてきたものです。通知からバンドルを取得できますが、メッセージ行の文字列配列を取得できません。これは、デバッガでは次のようになります。下の「バン」は、「通知」の「バンドル」です。 Debugger

私の質問はこれです。 "some message text - 4"で始まる文字列の配列を取得するにはどうすればよいですか?これはAndroidの通知のバンドルです。

答えて

0

私は最終的に、このように応答のための

CharSequence[] lines = bun.getCharSequenceArray(Notification.EXTRA_TEXT_LINES); 
    for(CharSequence line : lines){ 
     temp+= "line: " + line.toString() + "\n"; 
    } 
0

検索しようとしている文字列がArrayMap mMap(デバッガイメージに従って)に含まれているため、Mapの実装がシリアル化可能であるため、最初にそのマップを取得する必要があります。

ArrayMap map = bun.getSerializableExtra("mMap"); 

その後、次で(デバッガの画像に応じたCharSequenceオブジェクトの配列である)したい文字列の配列を得ることができる必要があります:

CharSequence[] messages = (CharSequence[]) map.valueAt(1); 

+0

おかげで、文字列を取得することができました。私はそれを試してみましたが、それはコンパイルされませんでした。しかし、うまくいけば正しい方向に導いています。調査は続行します... – jroal

+0

bun.getSerializableExtraをArrayMapにキャストする必要があるかもしれません。おそらくアンドロイドスタジオはあなたにそれがコンパイルされない理由を表示するでしょうか? –

関連する問題