catch on swipe to dismiss event

前端 未结 3 1325
耶瑟儿~
耶瑟儿~ 2020-11-27 12:22

I\'m using an android notification to alert the user once a service is finished (success or failure), and I want to delete local files once the process is done.

My p

3条回答
  •  孤街浪徒
    2020-11-27 12:36

    Another Idea:

    if you create a notification normally you also need the actions one, two or 3 of them. I've created a "NotifyManager" it creates all notifications i need and also receive all Intent calls. So i can manage all the actions AND also the catch the dismiss event at ONE place.

    public class NotifyPerformService extends IntentService {
    
    @Inject NotificationManager notificationManager;
    
    public NotifyPerformService() {
        super("NotifyService");
        ...//some Dagger stuff
    }
    
    @Override
    public void onHandleIntent(Intent intent) {
        notificationManager.performNotifyCall(intent);
    }
    

    to create the deleteIntent use this (in the NotificationManager):

    private PendingIntent createOnDismissedIntent(Context context) {
        Intent          intent          = new Intent(context, NotifyPerformMailService.class).setAction("ACTION_NOTIFY_DELETED");
        PendingIntent   pendingIntent   = PendingIntent.getService(context, SOME_NOTIFY_DELETED_ID, intent, 0);
    
        return pendingIntent;
    }
    

    and THAT i use to set the delete Intent like this (in the NotificationManager):

    private NotificationCompat.Builder setNotificationStandardValues(Context context, long when){
        String                          subText = "some string";
        NotificationCompat.Builder      builder = new NotificationCompat.Builder(context.getApplicationContext());
    
    
        builder
                .setLights(ContextUtils.getResourceColor(R.color.primary) , 1800, 3500) //Set the argb value that you would like the LED on the device to blink, as well as the rate
                .setAutoCancel(true)                                                    //Setting this flag will make it so the notification is automatically canceled when the user clicks it in the panel.
                .setWhen(when)                                                          //Set the time that the event occurred. Notifications in the panel are sorted by this time.
                .setVibrate(new long[]{1000, 1000})                                     //Set the vibration pattern to use.
    
                .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher))
                .setSmallIcon(R.drawable.ic_white_24dp)
                .setGroup(NOTIFY_GROUP)
                .setContentInfo(subText)
                .setDeleteIntent(createOnDismissedIntent(context))
        ;
    
        return builder;
    }
    

    and finally in the same NotificationManager is the perform function:

    public void performNotifyCall(Intent intent) {
        String  action  = intent.getAction();
        boolean success = false;
    
        if(action.equals(ACTION_DELETE)) {
            success = delete(...);
        }
    
        if(action.equals(ACTION_SHOW)) {
            success = showDetails(...);
        }
    
        if(action.equals("ACTION_NOTIFY_DELETED")) {
            success = true;
        }
    
    
        if(success == false){
            return;
        }
    
        //some cleaning stuff
    }
    

提交回复
热议问题