简体   繁体   中英

Changing a background image of a remoteView button?

Cant quite figure this one out. I have a button on my Activity that once pressed opens a notification. This custom notification has a button on it. I'm trying to change the notifications background image once its pressed.

Here's what I've used so far with no luck.

This is a service class that gets called once the "notifcation_Button" gets pressed. ( I have a pending intent that calls this class)

  public class newService extends Service {

  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {

    Log.d("newService", "Newservice");

    RemoteViews remoteView = new RemoteViews(getPackageName(), R.layout.custom_layout); //this is my custom notification

    remoteView.setInt(R.id.StartButton_Notification, "setBackgroundColor", R.drawable.sungrey);
    remoteView.setInt(R.id.StartButton_Notification, "setBackgroundResource", sungrey);
    remoteView.setImageViewResource(R.id.StartButton_Notification, R.drawable.sungrey);

    return START_NOT_STICKY;
}

I think in custom_layout.xml file you can give parent view an id. Get reference to that parent view using its id in your code and change background image of that view to achieve desired results.

So the issue for anyone wondering is that NotificationCompat.Builder isnt being updated, so where ever you have it originally, it needs to be updated with remoteView.setInt(R.id.yourDrawable, "setBackgroundResource", newDrawable);. Either toss it in a method and update it that way or bring the whole sucker into the class where the change happens.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM