RemoteView不会更改图像

时间:2014-08-25 10:25:57

标签: android service remoteview

我有一项服务,在启动时显示通知。通知看起来像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,但这也不起作用。
另一个黑客攻击是取消通知并一起显示一个新的通知,这似乎相当昂贵。

1 个答案:

答案 0 :(得分:0)

所以,稍微改变一下。我添加了一个新方法:

 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的通知会更新通知。