android - 如何更新自定义布局通知操作?

标签 android android-notifications android-pendingintent android-remoteview foreground-service

我有一个完全可用的自定义布局通知,但我不知道如何在特定情况下更新操作

我正在使用此通知在应用程序外启动和停止媒体播放器,但我需要某种更新操作(待定 Intent 按钮)

例如:每当我点击播放时,它应该添加停止 Action (用新 Action 更新通知)

代码如下:

@Override
public void onCreate() {
    super.onCreate();

    showNotification();
}

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

    if (intent.getAction().equals(Constants.ACTION.PLAY_ACTION)) {

        startPlaying();

    }else if (intent.getAction().equals(Constants.ACTION.STOP_ACTION)) {

        stopPlaying();
    }
    return START_STICKY;
}

private void showNotification() {

    // Using RemoteViews to bind custom layouts into Notification

    RemoteViews views = new RemoteViews(getPackageName(),
            R.layout.status_bar);
    RemoteViews bigViews = new RemoteViews(getPackageName(),
            R.layout.status_bar_expanded);


    views.setViewVisibility(R.id.status_bar_icon, View.VISIBLE);
    views.setViewVisibility(R.id.status_bar_album_art, View.GONE);
    bigViews.setImageViewBitmap(R.id.status_bar_album_art,
            Constants.getDefaultAlbumArt(this));

    Intent notificationIntent = new Intent(this, MainActivity.class);
    notificationIntent.setAction(Constants.ACTION.MAIN_ACTION);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
            | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
            notificationIntent, 0);

    Intent playIntent = new Intent(this, IZIGaapsCustomNotification.class);
    playIntent.setAction(Constants.ACTION.PLAY_ACTION);
    PendingIntent pplayIntent = PendingIntent.getService(this, 0,
            playIntent, 0);

    Intent nextIntent = new Intent(this, IZIGaapsCustomNotification.class);
    nextIntent.setAction(Constants.ACTION.STOP_ACTION);
    PendingIntent pnextIntent = PendingIntent.getService(this, 0,
            nextIntent, 0);


    //Whenever i hit play, beneath buttons (actions) should appear

    views.setOnClickPendingIntent(R.id.status_bar_play, pplayIntent);
    bigViews.setOnClickPendingIntent(R.id.status_bar_play, pplayIntent);


    //this should be appear next to above button(when above button clicked)

    views.setOnClickPendingIntent(R.id.status_bar_next, pstopIntent);
    bigViews.setOnClickPendingIntent(R.id.status_bar_next, pstopIntent);


    views.setTextViewText(R.id.status_bar_track_name, "Song Title");
    bigViews.setTextViewText(R.id.status_bar_track_name, "Song Title");

    views.setTextViewText(R.id.status_bar_artist_name, "Artist Name");
    bigViews.setTextViewText(R.id.status_bar_artist_name, "Artist Name");


    status = new Notification.Builder(this).build();
    status.contentView = views;
    status.bigContentView = bigViews;
    status.flags = Notification.FLAG_ONGOING_EVENT;
    status.icon = R.drawable.ic_launcher;
    status.contentIntent = pendingIntent;
    startForeground(Constants.NOTIFICATION_ID.FOREGROUND_SERVICE, status);
}

任何帮助将不胜感激

最佳答案

这里是一个如何更新自定义通知的例子

private static final int NOTIF_ID = 1234;
private NotificationCompat.Builder mBuilder;
private NotificationManager mNotificationManager;
private RemoteViews mRemoteViews;
private Notification mNotification;
private void setUpNotification(){

    mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    // we need to build a basic notification first, then update it
    Intent intentNotif = new Intent(this, MainActivity.class);
    intentNotif.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent pendIntent = PendingIntent.getActivity(this, 0, intentNotif, PendingIntent.FLAG_UPDATE_CURRENT);
Intent playIntent = new Intent(this, IZIGaapsCustomNotification.class);
playIntent.setAction(Constants.ACTION.PLAY_ACTION);
PendingIntent pplayIntent = PendingIntent.getService(this, 0,
        playIntent, 0);
    // notification's layout
    mRemoteViews = new RemoteViews(getPackageName(), R.layout.custom_notification_small);
    // notification's icon
    mRemoteViews.setImageViewResource(R.id.status_bar_play, R.drawable.play);
mRemoteViews.setOnClickPendingIntent(R.id.status_bar_play, pplayIntent);

    mBuilder = new NotificationCompat.Builder(this);

    CharSequence ticker = getResources().getString(R.string.ticker_text);
    int apiVersion = Build.VERSION.SDK_INT;

    if (apiVersion < VERSION_CODES.HONEYCOMB) {
        mNotification = new Notification(R.drawable.ic_launcher, ticker, System.currentTimeMillis());
        mNotification.contentView = mRemoteViews;
        mNotification.contentIntent = pendIntent;

        mNotification.flags |= Notification.FLAG_NO_CLEAR; //Do not clear the notification
        mNotification.defaults |= Notification.DEFAULT_LIGHTS;

        // starting service with notification in foreground mode
        startForeground(NOTIF_ID, mNotification);

    }else if (apiVersion >= VERSION_CODES.HONEYCOMB) {
        mBuilder.setSmallIcon(R.drawable.ic_launcher)
                .setAutoCancel(false)
                .setOngoing(true)
                .setContentIntent(pendIntent)
                .setContent(mRemoteViews)
                .setTicker(ticker);

        // starting service with notification in foreground mode
        startForeground(NOTIF_ID, mBuilder.build());
    }
}

使用此方法更新您的接收器中的通知 UI 以播放或暂停将操作发送到此方法

 private void updateNotification(String action ){

        int api = Build.VERSION.SDK_INT;
        //update action 
 Intent nextIntent = new Intent(this, IZIGaapsCustomNotification.class);
    nextIntent.setAction(action);
    PendingIntent pnextIntent = PendingIntent.getService(this, 0,
            nextIntent, 0);
        // update the icon
        mRemoteViews.setImageViewResource(R.id.status_bar_play, R.drawable.icon_off2);

mRemoteViews.setOnClickPendingIntent(R.id.status_bar_play, pnextIntent);
        // update the notification
        if (api < VERSION_CODES.HONEYCOMB) {
            mNotificationManager.notify(NOTIF_ID, mNotification);
        }else if (api >= VERSION_CODES.HONEYCOMB) {
            mNotificationManager.notify(NOTIF_ID, mBuilder.build());
        }
    }

关于android - 如何更新自定义布局通知操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47788150/

相关文章:

c# - 未设置 Xamarin Forms 文件提供程序

android - Flutter Android 将 alarmmanager 与通知相结合

Android - 为什么对地理围栏使用未决 Intent

android - 当应用程序从后台清除时,AlarmManager 被丢弃

java - 备份/恢复整个 Eclipse 工作区

Android - 我无法刷新/重绘 ListView

android - 如何更改 SearchView 文本颜色?

android - 如何计算通知数量并在Android中显示单个图标?

android - 以编程方式单击通知

android - Intent - 如果 Activity 正在运行,请将其放在前面,否则启动一个新 Activity (来自通知)