Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

stop service in android

Here I tried simple service program. Start service works fine and generates Toast but stop service does not. The code of this simple service is as below:

public class MailService extends Service {     @Override     public IBinder onBind(Intent arg0) {         // TODO Auto-generated method stub         return null;     }     public void onCreate(){         super.onCreate();         Toast.makeText(this, "Service Started", Toast.LENGTH_SHORT).show();     }     public void onDestroyed(){         Toast.makeText(this, "Service Destroyed", Toast.LENGTH_SHORT).show();         super.onDestroy();     } } 

The code of the Activity from where this Service is called is as below:

public class ServiceTest extends Activity{     private Button start,stop;      public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.service_test);          start=(Button)findViewById(R.id.btnStart);         stop=(Button)findViewById(R.id.btnStop);          start.setOnClickListener(new View.OnClickListener() {              @Override             public void onClick(View v) {                 // TODO Auto-generated method stub                 startService(new Intent(ServiceTest.this,MailService.class));             }         });         stop.setOnClickListener(new View.OnClickListener() {              @Override             public void onClick(View v) {                 // TODO Auto-generated method stub                 stopService(new Intent(ServiceTest.this,MailService.class));             }         });     } } 

Help me to stop service with that stop button which generates toast in the onDestroy() method. I have already seen many posts regarding stop service problem here, but not satisfactory so posting new question. Hope for satisfactory answer.

like image 746
Ravi Bhatt Avatar asked Apr 05 '11 17:04

Ravi Bhatt


People also ask

How do I stop a service started?

A started service must manage its own lifecycle. That is, the system doesn't stop or destroy the service unless it must recover system memory and the service continues to run after onStartCommand() returns. The service must stop itself by calling stopSelf() , or another component can stop it by calling stopService() .

How can we stop the services in Android 1 point?

We can stop the services by stopSelf() and stopService(), in some cases android will kill the services due to the low memory problem.

How do I stop a service explicitly?

Fundamentals of Android Services it can be stopped explicitly using stopService() or stopSelf() methods. with the service effectively by returning an IBinder object. If the binding of service is not required then the method must return null.

How do I stop programmatically running in the background Android?

stopSelf() is used to always stop the current service. stopSelf(int startId) is also used to stop the current service, but only if startId was the ID specified the last time the service was started. stopService(Intent service) is used to stop services, but from outside the service to be stopped.


2 Answers

onDestroyed() 

is wrong name for

onDestroy()   

Did you make a mistake only in this question or in your code too?

like image 196
kreker Avatar answered Sep 26 '22 08:09

kreker


This code works for me: check this link
This is my code when i stop and start service in activity

case R.id.buttonStart:   Log.d(TAG, "onClick: starting srvice");   startService(new Intent(this, MyService.class));   break; case R.id.buttonStop:   Log.d(TAG, "onClick: stopping srvice");   stopService(new Intent(this, MyService.class));   break; } }  } 

And in service class:

  @Override public void onCreate() {     Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();     Log.d(TAG, "onCreate");      player = MediaPlayer.create(this, R.raw.braincandy);     player.setLooping(false); // Set looping }  @Override public void onDestroy() {     Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();     Log.d(TAG, "onDestroy");     player.stop(); } 

HAPPY CODING!

like image 34
dondondon Avatar answered Sep 23 '22 08:09

dondondon