2

Ich erstelle eine Android-Apps, wo eine Benachrichtigung an den Benutzer jeden Tag zu einer bestimmten Zeit gesendet wird. Die Benachrichtigung wird im Hintergrund ausgeführt. Ich möchte, dass meine Apps den Benutzer über die Schaltfläche btnStopService ausschalten lassen. Wie kann ich das erreichen? Ich habe es versucht, aber die Benachrichtigung kann nicht ausgeschaltet werden.So deaktivieren Sie die Benachrichtigung im Hintergrunddienst ausgeführt

Hinweis: Angenommen, ich rufe die startNotification-Methode zu einem bestimmten Zeitpunkt auf. Ignoriere die for-Schleife innerhalb der startNotification-Methode.

Dies sind die Codes für die startNotification Methode:

public void startNotification() { 
    notifyManager = (NotificationManager) 
      getSystemService(Context.NOTIFICATION_SERVICE); 
    notifyBuilder = new NotificationCompat.Builder(getApplicationContext()); 

    notifyBuilder.setContentTitle("Good morning"); 
    notifyBuilder.setContentText("Morning"); 
    notifyBuilder.setSmallIcon(R.drawable.bmi_cal); 
    notifyBuilder.setPriority(Notification.PRIORITY_MAX); 
    notifyBuilder.setLights(Color.BLUE, 500, 500); 
    try { 
     Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
     Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification); 
     r.play(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    if (Build.VERSION.SDK_INT >= 21) notifyBuilder.setVibrate(new long[5000]); 

    new Thread(new Runnable() { 
     @Override 
     public void run() { 

      for(int i=0;i<1;i++) 
      { 
       notifyManager.notify(MY_NOTIFICATION_ID, notifyBuilder.build()); 

       try { 
        Thread.sleep(1 * 1000); 
       } catch (InterruptedException e) { 

       } 
      } 

      // for (i = 0; i <= 20; i += 10) { 
       //notifyBuilder.setProgress(100, i, false); 


      // } 

      //notifyBuilder.setContentText("Bread is ready!"); 

      //notifyBuilder.setProgress(0, 0, false); 

      notifyManager.notify(MY_NOTIFICATION_ID, notifyBuilder.build()); 
     } 
    } 
    ).start(); 

} 

Dies sind die Codes für die startCommand Methode sind die Dienste zu starten:

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    Thread triggerService=new Thread(new Runnable() { 
     long startingTime=System.currentTimeMillis(); 
     long tics=0; 
     @Override 
     public void run() { 

      for(;;) 
      { 

       try 
       { 
        // ASSUME I trigger the notification at a specific time 
        startNotification(); 



        /* tics= System.currentTimeMillis() - startingTime; 
        Intent myFilteredResponse=new Intent("liren.action.GOSERVICE3"); 
        String msg= " value: " + tics; 
        myFilteredResponse.putExtra("serviceData", msg); 
        sendBroadcast(myFilteredResponse); */ 
        Thread.sleep(3000); 
       } 
       catch (Exception e) 
       { 
        e.printStackTrace(); 
       } 
       //notifyManager.notify(MY_NOTIFICATION_ID, notifyBuilder.build()); 
      } 

     } 
    }); 
    triggerService.start(); 
    return super.onStartCommand(intent, flags, startId); 
} 

Dies sind die Codes innerhalb btnStopService Methode.

Die btnStopService-Methode befindet sich in der StopNotification-Java-Klasse.

public class StopNotification extends AppCompatActivity { 
    ComponentName service; 
    Intent intentMyService; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_stop_notification); 
     intentMyService = new Intent(this, myServiceRunner.class); 
    } 

    public void btnTurnOff(View v) 
    { 
     try { 
      stopService(new Intent(intentMyService)); 
      Thread.sleep(1000); 
      Toast.makeText(getApplicationContext(), "Turn Off Notification", Toast.LENGTH_SHORT).show(); 
      finish(); 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

    } 
} 

Antwort

1

Nur diese verwenden:

Intent intent = new Intent(this, YourService.class); 
stopService(intent);