首页 文章

在remoteview通知中更改Imageviewresource

提问于
浏览
2

我正在通过按钮和textview显示服务类内音乐播放器的通知 . 每当歌曲改变时文本就会更新,但我需要在点击播放按钮时更改ImageViewResource,它应该在音乐停止后更改为暂停 . 我面临的另一个问题是当textview文本更改整个通知视图刷新1秒然后只有文本更改 . 我怎么能停止整个视图的刷新?这是我到目前为止尝试的代码:

Service class:

@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);
}

1 回答

  • 2

    您必须通过remoteView访问ImageView视图并设置图像资源/位图 . 如果您之前需要下载图像,可以使用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) {
            }
        });
    

相关问题