0

Es scheint andere Fragen in Bezug auf die laufende Benachrichtigung zu löschen.
Allerdings schaue ich wirklich eine ganze Reihe von ihnen und habe immer noch keine Lösung.Android abbrechen laufende Benachrichtigung funktioniert nicht

SettingFragment 
public class SettingFragment extends Fragment { 
    private Switch mSwitchNotific; 
    mSwitchNotific.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
     @Override 
     public void onCheckedChanged(CompoundButton buttonView, 
            boolean isChecked) { 
      if(isChecked){ 
       Log.v(LOGTAG, "step notification on"); 
       getContext().startService(new Intent(getContext(), UpdateStepNotificationService.class)); 
      }else{ 
       Log.v(LOGTAG, "step notification off"); 
       NotificationManager notificationManager = (NotificationManager) 
         getContext().getSystemService(Context.NOTIFICATION_SERVICE); 
       notificationManager.cancel(StickyUpdateStepNotification.NOTIFICATION_ID); 
      } 
     } 
    } 
} 


UpdateStepNotificationService 
    public class UpdateStepNotificationService extends Service { 
     static NotificationManager notificationManger; 
     StepsDBHandler stepsDBHandler; 

     public UpdateStepNotificationService() {} 

     @Override 
     public void onCreate() { 
      super.onCreate(); 
      stepsDBHandler = new StepsDBHandler(getApplicationContext(), null, null, 0); 
      notificationManger = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); 
      PendingIntent pendingIntent = PendingIntent.getActivity(this, 
       StickyUpdateStepNotification.NOTIFICATION_ID, 
       new Intent(getApplicationContext(), MainActivity.class), 
       PendingIntent.FLAG_UPDATE_CURRENT); 

      final Notification notification = new Notification.Builder(
      getApplicationContext()) 
       .setSmallIcon(R.drawable.ic_notice) 
       .setContentTitle("Walksapp") 
       .setContentText("Now: " +Integer.toString(stepsDBHandler.getCurrentStepToday()) + " steps") 
      .setContentIntent(pendingIntent) 
      .build(); 
      notification.flags = Notification.FLAG_NO_CLEAR | Notification.FLAG_ONGOING_EVENT; 

      new Timer().schedule(new TimerTask() { 
       @Override 
       public void run() { 
       notificationManger.notify(StickyUpdateStepNotification.NOTIFICATION_ID, notification); 
       } 
      }, 0, 1000); 
     } 

     @Override 
     public int onStartCommand(Intent intent, int flags, int startId) { 
      super.onStartCommand(intent, flags, startId); 
      return START_STICKY; 
     } 

     @Override 
     public void onDestroy() { 
     notificationManger.cancel(StickyUpdateStepNotification.NOTIFICATION_ID); 
     super.onDestroy(); 
     } 
} 


UpdateStepNotificationService für Zählung mit den neuesten Schritt neue Meldung zu wiederholen erteilen.
Die Benachrichtigung erfolgt mit FLAG_NO_CLEAR und FLAG_ONGOING_EVENT.
Die einzige Möglichkeit, die Benachrichtigung ein- und auszuschalten, ist die Schaltfläche SettingFragment.

Versuch:
notificationManger.cancel(StickyUpdateStepNotification.NOTIFICATION_ID);
Problem

Hoffnung jede Eingabe sehen
Dank

+0

Sie müssen den Timer beenden, sonst wird die Benachrichtigung fortgesetzt. – dharms

+0

@dharms Sie sind Genie. Das habe ich vermisst. Sie können das einreichen und ich werde Ihre Antwort akzeptieren. – john

Antwort

1

Sie müssen den Timer töten sonst wird es weiterhin die Meldung aktualisieren.