Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
170 views
in Technique[技术] by (71.8m points)

android - How to use Notification.deleteIntent

I'm trying to detect when my notification gets cleared. My question directly refers to this answer which outlines what I'm suppose to do. This is how I'm implementing the actions:

// usual Notification initialization here
notification.deleteIntent = PendingIntent.getService(context, 0, new Intent(context, CleanUpIntent.class), 0);
notificationManager.notify(123, notification)

This is the CleanUpIntent class:

class CleanUpIntent extends IntentService {
    public CleanUpIntent() {
        super("CleanUpIntent");
    }

    @Override
    protected void onHandleIntent(Intent arg0) {
        // clean up code
    }
}

Afterwards, I simply launch the notification like I normally would but when I go to test it out (pressing "Clear All Notifications") nothing happens. I inserted a line of code that out print something to LogCat when the IntentService gets started, but nothing ever ran. Is this how I'm suppose to use Notification.deleteIntent?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

sample code which will be called whenever user clears the notification, hope it will help you .

 ....
 notificationBuilder.setDeleteIntent(getDeleteIntent());
 ....


protected PendingIntent getDeleteIntent()
 {
    Intent intent = new Intent(mContext, NotificationBroadcastReceiver.class);
    intent.setAction("notification_cancelled");
    return PendingIntent.getBroadcast(mContext, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
}

NotificationBroadcastReceiver.java

@Override
    public void onReceive(Context context, Intent intent)
    {
        String action = intent.getAction();
        if(action.equals("notification_cancelled"))
        {
            // your code
        }
    }

AndroidManifiest.xml

 <receiver android:name=".NotificationBroadcastReceiver">
                <intent-filter>
                    <action android:name="notification_cancelled"/>
                </intent-filter>
            </receiver>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...