gpt4 book ai didi

java - 单击时更改通知按钮图标

转载 作者:行者123 更新时间:2023-12-04 13:57:42 27 4
gpt4 key购买 nike

我为我的通知做了一个自定义布局,它有两个按钮,一个用于播放,一个用于暂停音乐。当我单击每个按钮时,会向相应的类(class)发送广播。

notification

现在我只想保留一个按钮并(切换)当用户点击按钮从播放到暂停时更改按钮的图标,反之亦然,我尝试了几件事 更改图标 (我真正的问题)但到目前为止失败了。

在 PlayerActivity.java 中,我通过调用此行来显示通知。

NotificationGenerator.customBigNotification(getApplicationContext());

这是 NotifcationGenerator 的代码:
package com.example.user.musicplayer;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.v4.app.NotificationCompat;
import android.widget.RemoteViews;

public class NotificationGenerator {
private static final int NOTIFICATION_ID_OPEN_ACTIVITY = 1;
private static final int NOTIFICATION_ID_CUSTOM_BIG = 1;

public static void customBigNotification(Context context){
RemoteViews expandedView = new RemoteViews(context.getPackageName(), R.layout.big_notification);
NotificationCompat.Builder nc = new NotificationCompat.Builder(context);
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent notifyIntent = new Intent(context, PlayerActivity.class);
notifyIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
notifyIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);


String id = "mymusicplayer";
CharSequence name = "player";
String description = "player";
int importance = NotificationManager.IMPORTANCE_LOW;

NotificationChannel mChannel = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
mChannel = new NotificationChannel(id, name, importance);
mChannel.setDescription(description);

mChannel.enableLights(true);
mChannel.setLightColor(Color.RED);

mChannel.enableVibration(true);
mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});

nm.createNotificationChannel(mChannel);
}



nc.setContentIntent(pendingIntent);
nc.setSmallIcon(R.drawable.ic_action_play);
nc.setAutoCancel(true);
nc.setCustomBigContentView(expandedView);
nc.setContentTitle("Music Player");
nc.setContentText("Control Audio");
nc.getBigContentView().setTextViewText(R.id.textSongName, "Lorem Ipsum Dolor");

setRemoteViews(expandedView, context);
nm.notify(NOTIFICATION_ID_CUSTOM_BIG, nc.setChannelId(id).build());
}

private static void setRemoteViews(RemoteViews remoteViews, Context c) {

// call broadcast when any control of notification is clicked.
Intent playIntent = new Intent(c, PlayBroadcast.class);

PendingIntent playPendingIntent = PendingIntent.getBroadcast(c, 0, playIntent, 0);

// Using RemoteViews to bind custom layouts into Notification
remoteViews.setOnClickPendingIntent(R.id.btnPlay, playPendingIntent);

// call broadcast when any control of notification is clicked.
Intent pauseIntent = new Intent(c, PauseBroadcast.class);

PendingIntent pausePendingIntent = PendingIntent.getBroadcast(c, 0, pauseIntent, 0);

// Using RemoteViews to bind custom layouts into Notification
remoteViews.setOnClickPendingIntent(R.id.btnPause, pausePendingIntent);
}

}

这是 PlayBroadCast.java 的代码:(从播放按钮接收广播的地方):
package com.example.user.musicplayer;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;

public class PlayBroadcast extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
MediaPlayer mP = PlayerActivity.mediaPlayer;
if(!mP.isPlaying()){
mP.start();

}
}
}

如果解释不清楚,请原谅我,因为如果我可能有正确的话来解释它。我会用谷歌搜索它。提前致谢。

最佳答案

尝试这个:

expandedLayout.setInt(R.id.notif_button_play, "setImageResource", R.drawable.ic_play)
在我的情况下, View 是 ImageButton,所以我通过“setImageResource”更改了图标。您可以设置 View 的函数名称。

关于java - 单击时更改通知按钮图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58569740/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com