Android 4.0 custom notification like google music

大憨熊 提交于 2019-12-07 04:23:25

问题


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

Screenshot:

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 ?


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/8711966/android-4-0-custom-notification-like-google-music

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!