问题描述
我想创建一个进度条和一个删除按钮自定义通知。
I want to create a custom notification with a progress bar and a delete button.
截图:
我成功创建了进度条的自定义通知,但我没能创建删除事件。我想取消的通知,并停止上传,一旦用户点击X(如谷歌音乐,所以我不希望启动的活性,清除通知)。
I succeed to create a custom notification with a progress bar, but I did not manage to create the delete event. I want to cancel the notification and stop the upload once the user click on the "x" (as in google music, so i don't want to start an activity to clear the notification).
这是我的工作code:
Here is my working code :
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);
我试图用广播接收器没有广播,但我不知道这是否是正确的方式来做到这一点,我没有成功,使其工作。我应该怎么使用,如何使用它?
I tried with a BroadCast receiver without "broadcasting" but I don't know if it's the right way to do it and I didn't succeed to make it work. What should I use and how to use it ?
推荐答案
我不知道它是如何制成的谷歌音乐,但你可以设置onClickListener在布局传递给 RemoteViews 。只是这样做:
I'm not sure how it's made in google music, but you can set onClickListener for any view in layout that passed to RemoteViews
. Just do like this:
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
和停止你想要什么,并取消该通知。
and create broadcast, that receives CANCEL_BROADCAST_ACTION
and stops what you want, and cancel this notification.
这篇关于像谷歌音乐的Android 4.0自定义通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!