我正在使用按钮和 TextView 在服务类内显示音乐播放器的通知。每当歌曲发生变化时,文本都会更新,但我需要在单击播放按钮时更改 ImageViewResource,一旦音乐停止,它应该更改为暂停。 我面临的另一个问题是,当 textview 文本更改时,整个通知 View 刷新 1 秒,然后仅文本更改。我怎样才能停止整个 View 的刷新?这是我到目前为止尝试过的代码:
服务等级:
@Override
public int onStartCommand(Intent intent,int flags,int startId)
{
if(intent!=null)
{
String action = intent.getAction();
if(!TextUtils.isEmpty(action))
{
if(action.equals(ACTION_PAUSE))
{
remoteView.setImageViewResource(R.id.btnPlay_notif,R.drawable.btn_pause); //changing imageview frm play to pause
AppWidgetManager manager = AppWidgetManager.getInstance(getApplicationContext());
manager.updateAppWidget(R.id.btnPlay_notif, remoteView); // is the app widget id given correct?
pausePlayer();
}}
return 1;
}
@Override
public void onPrepared(MediaPlayer mp) {
mp.start;
Context context = getApplicationContext();
remoteView = new RemoteViews(context.getPackageName(),
R.layout.notification);
remoteView.setTextViewText(R.id.tv_song_title_notif, songTitle);
}
最佳答案
必须通过remoteView访问ImageView View 并设置图像资源/位图。如果您之前需要下载图像,可以使用Universal Image Loader之类的库从URL中获取位图。
remoteView.setImageViewResource(R.id.yourImageView, R.drawable.yourResource);
nManager.notify(NOTIFICATION_PANEL_ID, notification);
或者
imageLoader.loadImage(imageUrl, options, new ImageLoadingListener() {
@Override
public void onLoadingStarted(String arg0, View arg1) {
}
@Override
public void onLoadingFailed(String arg0, View arg1, FailReason arg2) {
}
@Override
public void onLoadingComplete(String text, View view, Bitmap bitmap) {
remoteView.setImageViewBitmap(R.id.yourImageView, bitmap);
nManager.notify(NOTIFICATION_PANEL_ID, notification);
}
@Override
public void onLoadingCancelled(String arg0, View arg1) {
}
});
关于android - 在远程 View 通知中更改 Imageview 资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23999192/