【发布时间】:2014-10-13 17:48:47
【问题描述】:
目前我有一个播放音乐的服务。我想在服务开始播放音乐时显示通知。我就是这样做的:
public void setUpNotification() {
/*
* Set up the notification for the started service
*/
Notification.Builder notifyBuilder = new Notification.Builder(this);
notifyBuilder.setTicker("ticker");
notifyBuilder.setOngoing(true);
notifyBuilder.setContentTitle("title");
notifyBuilder.setContentInfo("content info");
// set up an Intent if the user clicks the notification
int NOTIFICATION_ID = 1;
Intent notifyIntent = new Intent(this, MainActivity.class);
notifyIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pi = PendingIntent.getActivity(this, 0, notifyIntent,
NOTIFICATION_ID);
notifyBuilder.setContentIntent(pi);
Notification notification = notifyBuilder.build();
// start ongoing
startForeground(NOTIFICATION_ID, notification);
}
该方法在服务 playMusic() 中被调用,如下所示:
public void playMusic(String songPath) {
if(mPlayer == null || !mPlayer.isPlaying()){
try {
mPlayer = MediaPlayer.create(getBaseContext(), Uri.parse(songPath));
mPlayer.start();
this.songPath = songPath;
} catch (IllegalStateException e) {
e.printStackTrace();
}
}else{
mPlayer.stop();
mPlayer.release();
mPlayer = MediaPlayer
.create(getBaseContext(), Uri.parse(songPath));
mPlayer.start();
this.songPath = songPath;
}
setUpNotification();
}
问题在于未显示通知待处理意图、内容标题和内容信息。通知如下所示:
如您所见,标题和内容信息不会显示。此外,当我单击通知时,它不会触发我的待处理意图。
我在这里做错了什么?任何帮助是极大的赞赏。
马库斯
【问题讨论】:
标签: android android-intent android-notifications