Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

destroy an android service

I am using a service in my android app, which is called when an alarm is activated by a calander. When the service has been activated i want it to be destroyed by the OnStart() method once it has completed its code.

My OnStart() method:

@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);

Vibrator vi = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

vi.vibrate(5000);

Toast.makeText(this, "MyAlarmService.onStart()", Toast.LENGTH_LONG).show();

    //CODE HERE TO DESTROY SERVICE??

}

This bassically means when the service is called it runs the code in the OnStart() method and i want it to destroy itself. Any Ideas, methods that would do this?.

Thanks, jack.

like image 627
Jack Trowbridge Avatar asked Dec 15 '22 22:12

Jack Trowbridge


1 Answers

You can call stopSelf() in onCreate to stop the service

like image 100
rajpara Avatar answered Feb 14 '23 06:02

rajpara