Android 4.0 special notification e.g. google music

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

Screenshot:

enter image description here

I was able to create a custom notification with a progress bar, but I was not able to create a delete event. I want to cancel the notification and stop the download as soon as the user presses "x" (as in google music, so I do not want to start the operation 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);

In line

Intent notificationIntent = new Intent(context, UploadEventsReceiver.class);

I tried to use the BroadCast receiver without "broadcasting," but I don’t know if it did it right, and I couldn’t get it working. What should I use and how to use it?

+5
source share
2 answers

, google, onClickListener , RemoteViews. :

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

+5

, ( ), .

sth :

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

0

All Articles