我有一个服务,它在启动时显示通知。通知看起来像MediaPlayer控件。单击“暂停”按钮时,再次调用Service,媒体播放器暂停。再次单击时,它将继续播放。然而,图像不切换。这是我的代码:
@Override
public int onStartCommand(Intent intent, int flags, int startId){
MediaPlayer player = PodcastrApplication.newInstance().getMediaPlayer();
request = intent.getIntExtra(REQUEST_CODE, 0);
if(request == PAUSE){
// Toggle Play and Pause
if(player.isPlaying()){
player.pause();
rm.setImageViewResource(R.id.pause, R.drawable.ic_action_play);
}else{
player.start();
rm.setImageViewResource(R.id.pause, R.drawable.ic_action_pause);
}
}else if(request == DISMISS){
hideNotification();
stopSelf();
}
return START_STICKY;
} 为什么不起作用?
我认为这是一个线程问题,所以我甚至尝试使用Handler,但这也不起作用。
另一种方法是取消通知并同时显示一个新的通知,这似乎非常昂贵。
发布于 2014-08-25 10:52:59
所以,稍微改变一下。我添加了一个新方法:
private void updateNotification(){
NotificationManager mgr = (NotificationManager) getApplicationContext()
.getSystemService(Context.NOTIFICATION_SERVICE);
MediaPlayer player = PodcastrApplication.newInstance().getMediaPlayer();
if(player.isPlaying()){
rm.setImageViewResource(R.id.pause, R.drawable.ic_action_play);
}else{
rm.setImageViewResource(R.id.pause, R.drawable.ic_action_pause);
}
Notification notif = new NotificationCompat.Builder(getApplicationContext())
.setOngoing(true)
.setSmallIcon(R.drawable.ic_launcher)
.setContent(rm)
.build();
mgr.notify(NOTIFICATION_ID, notif);
} 重新发出具有相同ID的通知将更新通知。
https://stackoverflow.com/questions/25483825
复制相似问题