【问题标题】:how to update custom layout notification actions?如何更新自定义布局通知操作?
【发布时间】:2018-05-27 01:19:33
【问题描述】:

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

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

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

代码如下:

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

}

任何帮助将不胜感激

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

    • ok @tamtom 我测试了它,它已经弃用了任何更好的解决方案?我的目标是 api 21 到上面
    • 什么是不推荐使用的方法?
    • @pourya011 你必须添加频道 id 这是 Android Oreo Builder(MainActivity.this, "channel_id") 检查这个答案以进一步阅读 stackoverflow.com/questions/45462666/… 并且总是如果你看到任何不推荐使用的方法在谷歌上搜索它的名字看看是什么替换。如果您需要其他帮助,请告诉我!
    • 再次感谢您的宝贵时间,当然,让我先检查您发布的链接
    • @pourya011 如果有帮助,请将其标记为正确答案。
    猜你喜欢
    • 2015-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-12
    • 1970-01-01
    相关资源
    最近更新 更多