5

I want to create a custom notification with a progress bar and a delete button.

Screenshot:

enter image description here

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).

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);

At the line

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 ?

AakashM
  • 62,551
  • 17
  • 151
  • 186
Quanturium
  • 5,698
  • 2
  • 30
  • 36
  • Nice, I was about to implement what I just found in your screenshot: neat hardware control toggles, e.g. for WiFi, rotation, or flight mode. Are you running a custom ROM or did you find that on the Market? Thanks for your help and good luck with your project. – stfn Jan 03 '12 at 16:48

2 Answers2

5

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);

and create broadcast, that receives CANCEL_BROADCAST_ACTION and stops what you want, and cancel this notification.

Jin35
  • 8,602
  • 3
  • 32
  • 52
  • Will the intent be broadcasted ? Or will the intend be only send to my cancel_broadcast ? – Quanturium Jan 03 '12 at 13:10
  • It depends on value of `CANCEL_BROADCAST_ACTION` - if it is custom(smth like "lalala") - only your broadcast will receive this intent – Jin35 Jan 03 '12 at 13:28
0

Also, I think (according to the documentation) that your method is deprecated.

You should use sth like:

Notification.Builder builder = new Notification.Builder(context);
builder.setContentTitle("Your notification title");
builder.setContentText("Your notification text");
builder.setSmallIcon(R.drawable.ic_your_icon);
builder.setContentIntent(yourIntent);
notificationManager.notify(null, YOUR_UNIQUE_NOTIFICATION_ID, builder.getNotification());

http://developer.android.com/reference/android/app/NotificationManager.html

http://developer.android.com/reference/android/app/Notification.Builder.html

Martin Bories
  • 1,067
  • 17
  • 37
  • 1
    Note: Notification.Builder is available only in API 11+. The v4 support library has NotificationBuilderCompat, but according to the following link it is currently missing its setProgress method. http://code.google.com/p/android/issues/detail?id=30755 i – MaximumGoat Jul 06 '12 at 06:35