How to disable notification running in background service

I am creating an android Apps where a notification will be sent to user everyday at a specific time. The notification is running in the background. I would want my Apps to let the user turn off the notification using a button called btnStopService. How can I achieve that? I tried but the notification cannot be turned off.

Note: Assume I am calling the startNotification method at a specific time. Ignore the for loop inside startNotification method.

These are the codes for the startNotification method:

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();

}

These are the codes for the startCommand method to start the services:

@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);
}

These are the codes inside btnStopService method.

btnStopService method is inside StopNotification java class.

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();
        }

    }
}

Upvotes: 2

Views: 367

Answers (1)

Gautam Malik
Gautam Malik

Reputation: 514

Just use this :

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

Upvotes: 1

Related Questions