我想创建一个带有进度条和删除按钮的自定义通知。
截图:
我成功地创建了带有进度条的自定义通知,但我没有设法创建删除事件。我想在用户点击“x”后取消通知并停止上传(就像在谷歌音乐中一样,所以我不想启动 Activity 来清除通知)。
这是我的工作代码:
notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
int icon = R.drawable.ic_launcher;
CharSequence tickerText = "Hello";
long when = System.currentTimeMillis();
notification = new Notification(icon, tickerText, when);
CharSequence contentTitle = "My notification";
CharSequence contentText = "Hello World!";
Intent notificationIntent = new Intent(context, UploadEventsReceiver.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
notification.contentView = new RemoteViews(context.getPackageName(), R.layout.upload_progress);
notification.contentView.setOnClickPendingIntent(R.id.text, contentIntent);
notificationManager.notify(this.notificationId, notification);
在线
Intent notificationIntent = new Intent(context, UploadEventsReceiver.class);
我尝试使用没有“广播”的 BroadCast 接收器,但我不知道这样做是否正确,但我没有成功使其工作。我应该使用什么以及如何使用它?
最佳答案
我不确定它是如何在 google music 中制作的,但您可以为传递给 RemoteViews
的布局中的任何 View 设置 onClickListener。就像这样:
RemoteViews remoteViews = new RemoteViews(widgetPackage, R.layout.widget);
Intent action = new Intent(context, Your_cancel_broadcast.class);
action.setAction(CANCEL_BROADCAST_ACTION);
actionPendingIntent = PendingIntent.getBroadcast(context, 0, action, 0);
remoteViews.setOnClickPendingIntent(R.id.your_x_btn, actionPendingIntent);
并创建广播,接收 CANCEL_BROADCAST_ACTION
并停止您想要的内容,并取消此通知。
关于Android 4.0 自定义通知像谷歌音乐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8711966/