单击更改通知按钮图标

时间:2019-10-26 09:55:34

标签: java android android-notifications

我为通知创建了自定义布局,它有两个按钮,一个用于播放,一个用于暂停音乐。当我单击每个按钮时,广播将发送到相应的班级。

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

        }
    }
}

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

1 个答案:

答案 0 :(得分:0)

试试这个:

expandedLayout.setInt(R.id.notif_button_play, "setImageResource", R.drawable.ic_play)

就我而言,视图是 ImageButton,所以我通过“setImageResource”更改了图标。您可以设置视图的函数名称。