gpt4 book ai didi

android - 通知不可滑动以关闭

转载 作者:行者123 更新时间:2023-11-29 19:06:08 26 4
gpt4 key购买 nike

我正在我的 Android 应用程序中创建一个通知,但我希望当用户像所有其他简单通知一样向左或向右滑动通知时,通知从通知栏中清除。我已经尝试过我的东西,但它没有用。这是我的通知代码

private void showNotificationPause() {
// Using RemoteViews to bind custom layouts into Notification
RemoteViews views = new RemoteViews(getPackageName(),
R.layout.status_bar_old);
RemoteViews bigViews = new RemoteViews(getPackageName(),
R.layout.status_bar_expanded);
// showing default album image
views.setViewVisibility(R.id.status_bar_icon, View.GONE);
views.setViewVisibility(R.id.status_bar_album_art, View.VISIBLE);
bigViews.setImageViewBitmap(R.id.status_bar_album_art,
Constants.getDefaultAlbumArt(this));

Intent notificationIntent = new Intent(this, MusicActivity.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 previousIntent = new Intent(this, NotificationService.class);
previousIntent.setAction(Constants.ACTION.PREV_ACTION);
PendingIntent ppreviousIntent = PendingIntent.getService(this, 0,
previousIntent, 0);

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

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

Intent closeIntent = new Intent(this, NotificationService.class);
closeIntent.setAction(Constants.ACTION.STOPFOREGROUND_ACTION);
PendingIntent pcloseIntent = PendingIntent.getService(this, 0,
closeIntent, 0);

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

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

views.setOnClickPendingIntent(R.id.status_bar_prev, ppreviousIntent);
bigViews.setOnClickPendingIntent(R.id.status_bar_prev, ppreviousIntent);

views.setOnClickPendingIntent(R.id.status_bar_collapse, pcloseIntent);
bigViews.setOnClickPendingIntent(R.id.status_bar_collapse, pcloseIntent);

views.setImageViewResource(R.id.status_bar_play,
R.drawable.play);
bigViews.setImageViewResource(R.id.status_bar_play,
R.drawable.play);

views.setTextViewText(R.id.status_bar_track_name, song_name.get(pos));
bigViews.setTextViewText(R.id.status_bar_track_name, song_name.get(pos));

views.setTextViewText(R.id.status_bar_artist_name, singer.get(pos));
bigViews.setTextViewText(R.id.status_bar_artist_name, singer.get(pos));

bigViews.setTextViewText(R.id.status_bar_album_name, "");
status = new NotificationCompat.Builder(this)
.setAutoCancel(true)
.setOngoing(false)
.setContentIntent(PendingIntent.getActivity(getApplicationContext(),
Constants.NOTIFICATION_ID.FOREGROUND_SERVICE,
notificationIntent, 0))
.build();

status.flags = Notification.FLAG_AUTO_CANCEL;
// status = new Notification.Builder(this)
// .setAutoCancel(true)
// .setOngoing(false)
// .setContentIntent(PendingIntent.getActivity(getApplicationContext(), Constants.NOTIFICATION_ID.FOREGROUND_SERVICE, new Intent(), 0))
// .build();
status.contentView = views;
status.bigContentView = bigViews;
// status.flags = Notification.FLAG_AUTO_CANCEL;
// status.flags = Notification.FLAG_LOCAL_ONLY;
// status.flags = Notification.F;
status.icon = R.drawable.status;
status.contentIntent = pendingIntent;
Uri uri = Uri.parse(mySongs.get(pos));
NotificationTarget notificationTarget = new NotificationTarget(getApplicationContext(),
bigViews,
R.id.status_bar_album_art,
status,
101
);
NotificationTarget notificationTarget_sm = new NotificationTarget(getApplicationContext(),
views,
R.id.status_bar_album_art,
status,
101);

MediaMetadataRetriever mmr = new MediaMetadataRetriever();
try {
mmr.setDataSource(getApplicationContext(), Uri.parse(URLDecoder.decode(uri.toString())));
if (mmr.getEmbeddedPicture() != null) {
Glide
.with(getApplicationContext())
.load(mmr.getEmbeddedPicture())
.asBitmap()
.placeholder(R.drawable.logo2)
.into(notificationTarget_sm);
Glide
.with(getApplicationContext())
.load(mmr.getEmbeddedPicture())
.asBitmap()
.error(R.drawable.logo2)
.placeholder(R.drawable.logo2)
.into(notificationTarget);
}
}catch (Exception e){

}
NotificationManager notificationManager= (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(Constants.NOTIFICATION_ID.FOREGROUND_SERVICE, status);

// status.notify();
// startForeground(Constants.NOTIFICATION_ID.FOREGROUND_SERVICE, status);
}

我对 Notification 了解不多。有没有人知道发生了什么。

最佳答案

As indicated in comments , 你需要删除 startForeground因为这永远不会让用户删除通知,而是使用 NotificationManager#notify

关于android - 通知不可滑动以关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47159560/

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