作者showshowlay (咻咻雷)
看板AndroidDev
標題[問題] 監聽通知抓string
時間Mon Oct 15 15:42:22 2018
android studio 一個月新手
我現在想要在 NotificationListenerServic 抓到app通知的標題跟內容
抓到後回傳到其他activity,這個string另作其他用途
但目前試過intent回傳,程式點進去會閃退
目前onCreate如果加了char flag就會閃退,不加不會
想請問各位大大是什麼問題
Code 如下連結
https://ideone.com/fork/mnqVCs
activity的onCreate底下如下:
...
Bundle bundleNotify = this.getIntent().getExtras();
char flag = bundleNotify.getChar("nene"); //flag = 1 執行
String ChineseString = bundleNotify.getString("Title"+"Text");
//這行得到監聽的string,flag是有得到通知才會變成1,才送圖
if(flag==1) {
Bitmap Water = createChinese2(25, ChineseString);
Deliver(Water);
}
NotificationListenerService的副程式如下
public class NotificationService extends NotificationListenerService {
//收到通知時開始觸發
@Override
public void onNotificationPosted (StatusBarNotification sbn){
Notification mNotification = sbn.getNotification();//獲取通知包
Bundle bundle = new Bundle();
char flag =0;//flag
if(mNotification != null){
String packageName = sbn.getPackageName();//發送通知的包名
String notificationTitle = bundle.getString(mNotification.EXTRA_TITLE);//通知標題
String notificationText = bundle.getString(mNotification.EXTRA_TEXT);//通知內容
bundle.putString("Title",notificationTitle);
bundle.putString("Text",notificationText);
Intent intent = new Intent(this,StringAndPic.class);
intent.putExtras(bundle);
startActivity(intent); //
}
bundle.putChar("nene",flag);//當f=1執行
Intent intent = new Intent(this,StringAndPic.class);
intent.putExtras(bundle);
startActivity(intent);
}
--
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 60.248.185.175
※ 文章網址: https://www.ptt.cc/bbs/AndroidDev/M.1539589347.A.44A.html
推 zcbxvsdf: 有篇張貼程式碼的網站文章置頂,你找一個PO好嗎:) 10/15 16:30
→ showshowlay: 好 我貼一下 10/15 17:17
※ 編輯: showshowlay (60.248.185.175), 10/15/2018 17:41:38
推 aids61517: 有一個東西較Logcat,你可以看為什麼閃退 10/15 20:25
→ zcbxvsdf: 你看Logcat吧,你知道char是什麼嗎? 10/16 17:30
推 mewme: onCreate閃退的原因會不會是因為第34行 11/03 00:19
→ mewme: 這時bundle裡還沒有加putChar,所以get不到 11/03 00:21
推 opiopiuiop: 要不要檢查一下bundle是不是null 11/08 13:08