5
我正在嘗試通過控件爲音樂播放器發出通知。我正在成功偵聽按鈕點擊事件和功能正在正確觸發。只有我面臨的問題是改變這些點擊事件的通知文本。 這是我正在嘗試。動態更改Android通知文本
這是接收器成功接收呼叫並完美地發射每條線路。但我不能改變文本。我想我必須將內容視圖重置爲通知。如果是這樣,我該怎麼做?
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals("stop")) {
ABCFragment.stopSong();
Log.d("Notification","Stopping");
}else if (action.equals("play")) {
ABCFragment.togglePlayPause();
Log.d("Notification","Toggle Play/Pause");
RemoteViews contentView = new RemoteViews(context.getPackageName(),R.layout.notification_layout);
contentView.setTextViewText(R.id.songName, "SOME NEW SONG");
}else if (action.equals("next")) {
ABCFragment.playNextSong();
Log.d("Notification","Next");
}
}
解決方案:
我更新通知類的構造函數傳遞一個額外的參數,並得到它的工作!
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals("stop")) {
ABCFragment.stopSong();
Log.d("Notification","Stopping");
}else if (action.equals("play")) {
ABCFragment.togglePlayPause();
Log.d("Notification","Toggle Play/Pause");
new ABCNotification(context, "SOME NEW SONG");
}else if (action.equals("next")) {
ABCFragment.playNextSong();
Log.d("Notification","Next");
}
}
構造函數正在處理新傳遞的參數。
哦,現在所有文件都有意義。感謝您指出。我試過了,它的工作正常。我用解決方案更新了我的問題。 – unkn0wn 2014-09-13 10:23:35
另外請記住,如果你想要它更新漂亮,而不是擦除它並重新創建(這將使你的通知被擦除,並重新彈出列表中的其他地方),你需要使用builder.setOnlyAlertOnce(真正的)'以及你必須使用相同的建設者,否則它不會更新,它會做愚蠢的娛樂事情。 (我一直在爲此奮鬥了好幾天,但我似乎仍然無法讓它更新按鈕,除了現有的按鈕之外,它只會添加它們:-() – Jared 2014-09-19 01:19:34