[問題] 監聽通知抓string

看板AndroidDev作者 (咻咻雷)時間5年前 (2018/10/15 15:42), 5年前編輯推噓4(403)
留言7則, 5人參與, 5年前最新討論串1/1
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

10/15 16:30, 5年前 , 1F
有篇張貼程式碼的網站文章置頂,你找一個PO好嗎:)
10/15 16:30, 1F

10/15 17:17, 5年前 , 2F
好 我貼一下
10/15 17:17, 2F
※ 編輯: showshowlay (60.248.185.175), 10/15/2018 17:41:38

10/15 20:25, 5年前 , 3F
有一個東西較Logcat,你可以看為什麼閃退
10/15 20:25, 3F

10/16 17:30, 5年前 , 4F
你看Logcat吧,你知道char是什麼嗎?
10/16 17:30, 4F

11/03 00:19, 5年前 , 5F
onCreate閃退的原因會不會是因為第34行
11/03 00:19, 5F

11/03 00:21, 5年前 , 6F
這時bundle裡還沒有加putChar,所以get不到
11/03 00:21, 6F

11/08 13:08, 5年前 , 7F
要不要檢查一下bundle是不是null
11/08 13:08, 7F
文章代碼(AID): #1Rn4JZHA (AndroidDev)