0

Can any one tell me how to stop the service when the app get closed. In my project the service is still running in background even though the application got closed.Here i've given my sources for reference.

Suggestions please.

Thanks for your precious time!..

MainActivity.java

public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); startService(new Intent(this, MyService.class)); Calendar cal = Calendar.getInstance(); Intent intent = new Intent(this, MyService.class); PendingIntent pintent = PendingIntent .getService(this, 0, intent, 0); AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE); // Start service every 20 seconds alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 10* 1000, pintent); } } 

MyService.java

public class MyService extends Service { String result_data = ""; public MyService() { } @Override public IBinder onBind(Intent intent) { // TODO: Return the communication channel to the service. throw new UnsupportedOperationException("Not yet implemented"); } @Override public void onCreate() { // Toast.makeText(this, "The new Service was Created", Toast.LENGTH_LONG).show(); } @Override public void onStart(Intent intent, int startId) { // For time consuming an long tasks you can launch a new thread here... Toast.makeText(this, " Service Started", Toast.LENGTH_LONG).show(); Authentication_Class task = new Authentication_Class(); task.execute(); } @Override public void onDestroy() { Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show(); } public class Authentication_Class extends AsyncTask<Void, Void, Void> { @Override protected void onPreExecute() { // TODO Auto-generated method stub } @Override protected Void doInBackground(Void... params) { // TODO Auto-generated method stub try { Call_service(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } private void Call_service() throws Exception{ // TODO Auto-generated method stub System.setProperty("http.keepAlive", "false"); SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); envelope.dotNet = true; String URL = ""; String METHOD_NAME = ""; String NAMESPACE = ""; String SOAPACTION = ""; SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); // Add parameters if available envelope.setOutputSoapObject(request); HttpTransportSE httpTransportSE = new HttpTransportSE(URL,15000); try { httpTransportSE.call(SOAPACTION, envelope); SoapPrimitive result = (SoapPrimitive)envelope.getResponse(); result_data = String.valueOf(result); System.out.println(">>-- RESPONSE : " +result_data); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } } @Override protected void onPostExecute(Void result) { // TODO Auto-generated method stub } } 

}

1 Answer 1

1

Use this in your onDestroy() method in the activity where you are finishing your last activity like this:

 @Override protected void onDestroy() { super.onDestroy(); AppLog.Log(TAG, "On destroy"); stopService(new Intent(getApplicationContext(), MyService .class)); } 
Sign up to request clarification or add additional context in comments.

6 Comments

i've tried your suggestion, but its still running in background!
1 doubt, if i use the above onDestroy() in the same MainActivity.java, then what will happen?
put that code in the activity in which you want to stop service and if you are putting that in MainActivity.java then it will stopservice when activity got finished.
Can you tell me in what situation the method onDestroy() will be called?
On the above source,see MainActivity.java, im starting the service on oncreate() method, so that it is still running. But i dont know how to stop that service.do you have any idea?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.