我有自定义布局的完整工作通知,但我不知道如何在特定情况下更新操作
我使用此通知启动和停止应用程序外的媒体播放器,但我需要某种更新操作(待定意图按钮)
例如:每当我点击播放时,它应该添加停止操作(使用新操作更新通知)
这里是代码:
@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);
}
}
任何帮助将不胜感激
答案 0 :(得分: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());
}
}
使用此方法更新您的reciver中的通知用户界面以进行播放或暂停将操作发送到此方法
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());
}
}