Siju
Siju

Reputation: 2655

Changing Imageviewresource in remoteview notification

I am displaying notification for music player inside service class with buttons and textview. The text is getting updated whenever song changes but I need to change the ImageViewResource on click of play button it should change to pause once music stopped. Another issue what I am facing is when the textview text changes the whole notification view refreshes for 1 sec and then only text changes. How could I stop the refresh of whole view?This is the code I tried so far:

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

Upvotes: 2

Views: 2667

Answers (1)

Pelanes
Pelanes

Reputation: 3479

You must access the ImageView view through the remoteView and set the image resource/bitmap. If you need to donwload the image before, you can use a library like Universal Image Loader to get the bitmap from and URL.

remoteView.setImageViewResource(R.id.yourImageView, R.drawable.yourResource);
        nManager.notify(NOTIFICATION_PANEL_ID, notification);

OR

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

Upvotes: 4

Related Questions