Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android 4.0 custom notification like google music

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 ?

like image 619
Quanturium Avatar asked Jan 03 '12 12:01

Quanturium


1 Answers

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.

like image 187
Jin35 Avatar answered Sep 27 '22 18:09

Jin35