Android 4.0 自定义通知像谷歌音乐

标签 android notifications android-4.0-ice-cream-sandwich

我想创建一个带有进度条和删除按钮的自定义通知。

截图:

enter image description here

我成功地创建了带有进度条的自定义通知,但我没有设法创建删除事件。我想在用户点击“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/

相关文章:

android - 接收 Android 中多个特征的通知

TableLayout 的 Android 数据绑定(bind)

java - 使用 AsyncTask 将数据写入数据库更好吗?

android - 如果应用程序处于全屏模式,是否可以在通知栏上显示通知?

iphone - 我可以为 NSNotifications 注册一个类吗?我可以将类方法与 NSNotifications 一起使用吗?

android - SearchView展开抛出空指针异常

android - 如何更改 Android DatePicker 对话框的 "Dividers"颜色

android-wifi - 我可以从代码中打开 WiFi-Direct 吗?在 Android API-14 (ICS) 上

android - 快速启动命令

android - 如何在编辑过程中更改 Android 中 Android EditText 的背景和字体颜色?