title: onStartCommand Call after Destroying an Activity
body: <p>i'm observing a weird scenario here. I have a background android service which is running perfectly. but when I kill the <strong>process or application</strong> from my <code>RecentApps</code> my Application calls the <code>onStartCommand</code> method again. I don't know where I went wrong. I have searched alot but didn't find any appropriate solution. Could someone please mention what I did wrong? Thanks in Advance</p>
  
  <p><strong>Activity:</strong></p>
  
  <pre><code>public class OptionSelectionActivity extends Activity implements
      OnClickListener {
  
  Timer time;
  Intent serviceIntent;
  private Button btn_selectionquiz, btn_alerts, btn_history;
  ConnectionManager cm;
  boolean isInternetPresent = false;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      // TODO Auto-generated method stub
   Log.e("onCreate", "im Running");
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_option_selection);
  
      cm = new ConnectionManager(getApplicationContext());
      isInternetPresent = cm.isConnected();
  
      serviceIntent = new Intent(getApplicationContext(),MyService.class);
  //      serviceIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  
  //  isMyServiceRunning();
      if(!isMyServiceRunning())
      {
          Toast.makeText(getBaseContext(), "There is no service running, starting service..", Toast.LENGTH_SHORT).show();
          startService(serviceIntent);
      }else
      {
          Toast.makeText(getBaseContext(), "Service is already running", Toast.LENGTH_SHORT).show();          
      }
  
      XmlView();
      RegisterListenerOnXml();
  }
  
  private void XmlView() {
      btn_selectionquiz = (Button) findViewById(R.id.optionselection_btn_selectquiz);
      btn_alerts = (Button) findViewById(R.id.optionselection_btn_alerts);
      btn_history = (Button) findViewById(R.id.optionselection_btn_history);
  }
  
  private void RegisterListenerOnXml() {
      btn_selectionquiz.setOnClickListener(this);
      btn_alerts.setOnClickListener(this);
      btn_history.setOnClickListener(this);
  
  }
  
  @Override
  public void onClick(View v) {
      Intent i;
      // TODO Auto-generated method stub
      isInternetPresent = cm.isConnected();
      if(isInternetPresent)
      {
      switch (v.getId()) {
      case R.id.optionselection_btn_selectquiz:
          // intent calling
          i = new Intent(this, TeacherSelectionActivity.class);
          startActivity(i);
          break;
      case R.id.optionselection_btn_history:
          // intent calling
          i = new Intent(this, QuizHistoryActivity.class);
          startActivity(i);
          break;
  
      case R.id.optionselection_btn_alerts:
          // intent calling
          i = new Intent(this, GettingAlerts.class);
          startActivity(i);
          break;
      default:
          break;
      }
  }else
  {
      AlertDialogManager alert = new AlertDialogManager();
      alert.showAlertDialog(OptionSelectionActivity.this, "Internet Conncetion", "No internet Connection", false);
  }
  }
  
  @Override
  protected void onPause() {
      // TODO Auto-generated method stub
      super.onPause();
      if(!isMyServiceRunning())
      {
          Toast.makeText(getBaseContext(), "There is no service running, starting service..",      Toast.LENGTH_SHORT).show();
   //         startService(serviceIntent);
      }else
      {
          Toast.makeText(getBaseContext(), "Service is already running", Toast.LENGTH_SHORT).show();          
      }
  }
  
  private boolean isMyServiceRunning() {
      ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
      for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
          String temp = service.service.getClassName();
          if ("com.smartclasss.alerts.MyService".equals(temp)) {
              return true;
          }
      }
      return false;
  }
  
      @Override
  protected void onStop() {
      // TODO Auto-generated method stub
      super.onStop();
      Log.e("onSTOP", "im calling...!!!!");
      if(!isMyServiceRunning())
      {
          Toast.makeText(getBaseContext(), "There is no service running, starting service..", Toast.LENGTH_SHORT).show();
  //          startService(serviceIntent);
      }else
      {
          Toast.makeText(getBaseContext(), "Service is already running", Toast.LENGTH_SHORT).show();          
      }
  }
  
  @Override
  protected void onRestart() {
      // TODO Auto-generated method stub
      super.onRestart();
  
      Log.e("onRestart", "now im calling after onStop");
  }
  
   }
  </code></pre>
  
  <p><strong>Service:</strong></p>
  
  <pre><code> public class MyService extends Service{
  
  private SharedPreferences prefs; 
      private String prefName = "userPrefs";
  public static String GETTING_ALERTS_URL = "http://"
          + IPAddress.IP_Address.toString()
          + "//MyServices/Alerts/AlertService.svc/alert";
  public static String TAG_NAME = "DoitResult";
  public static String TAG_ALERT_TITLE = "alertTitle";
  static String Serv_Response = "";
  static String Serv_GettingQuiz_Response = "";
  boolean flag = false;
  boolean isServRun = true;
  public Timer time;
  ArrayList&lt;Alerts&gt; alertsList;
  public static final String INTENT_NOTIFY = "com.blundell.tut.service.INTENT_NOTIFY";
  // The system notification manager
  private NotificationManager mNM;
  
  @Override
  public IBinder onBind(Intent intent) {
      // TODO Auto-generated method stub
      return null;
  }
  
  
  
  
  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
  
  
      Log.e("Attendence", "Service Created");
      // TODO Auto-generated method stub
      time = new Timer();
      time.schedule(new TimerTask() {
  
          @Override
          public void run() {
              // TODO Auto-generated method stub
              DateFormat df = new SimpleDateFormat("dd-MM-yyyy");
              final String currentDate = df.format(Calendar.getInstance().getTime());
  //              Toast.makeText(getBaseContext(), "Service Started :"+" "+currentDate, Toast.LENGTH_LONG).show();
  
  
              if(flag == false)
              {
                  try {
                      savingDateinPref(currentDate);
  
  
                      new DoInBackground().execute(currentDate);  
                      flag = true;
                      isServRun = false;
                  } catch (Exception e) {
                      // TODO: handle exception
                      e.printStackTrace();
                  }
  
              }
              String val = prefs.getString("TAG_KEY", "defValue");
              if(!currentDate.equals(val))
              {
              flag = false;
              prefs = getSharedPreferences(prefName, MODE_PRIVATE); 
              SharedPreferences.Editor editor = prefs.edit(); 
  
              editor.remove("TAG_KEY");
               //---saves the values--- 
               editor.commit(); 
  
              }
          }
      },0,5000);
      return START_STICKY;
  }
  
  private class DoInBackground extends AsyncTask&lt;String, Void, Void&gt; {
  
      String cellphoneDate = "";
  
      ArrayList&lt;Alerts&gt; alertsList = new ArrayList&lt;Alerts&gt;();
      @Override
      protected Void doInBackground(String... params) {
          // TODO Auto-generated method stub
          cellphoneDate = params[0];
          DefaultHttpClient httpClient = new DefaultHttpClient();
          HttpGet httpGet = new HttpGet(GETTING_ALERTS_URL + "/"
                  + cellphoneDate);
          HttpResponse httpResponse = null;
          try {
              httpResponse = httpClient.execute(httpGet);
  
          } catch (ClientProtocolException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
  
          HttpEntity httpEntity = httpResponse.getEntity();
          try {
              Serv_Response = EntityUtils.toString(httpEntity);
          } catch (ParseException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
          try {
              if (Serv_Response != null) {
                  ////////////////////////////new code for getting list ///////////////////
                  JSONObject jsonObj1 = new JSONObject(Serv_Response);
                  JSONArray alertName = jsonObj1.getJSONArray(TAG_NAME);
  
                  for (int i = 0; i &lt; alertName.length(); i++) {
                      JSONObject c = alertName.getJSONObject(i);
                      String alert_title = c.getString(TAG_ALERT_TITLE);
                      Alerts alertObject = new Alerts();
                      alertObject.setAlertTitle(alert_title);
                      alertsList.add(alertObject);
                      }
  
              }
              }  catch (JSONException e) {
              // TODO: handle exception
              e.printStackTrace();
          }
          return null;
      }
      @Override
      protected void onPostExecute(Void result) {
          // TODO Auto-generated method stub
          super.onPostExecute(result);
      //  Toast.makeText(getBaseContext(), "From Database :" + Serv_GettingQuiz_Response,     Toast.LENGTH_LONG).show();
          //String array[] = new String[size]; 
          for(int i = 0; i &lt; alertsList.size() ; i++ )
          {
          showNotification(alertsList.get(i).getAlertTitle(), "TAP for More Details", i);
          //  savingDate(Serv_GettingQuiz_Response);
          }
      }
  
  }
  
  private void savingDateinPref(String value){
  
      prefs = getSharedPreferences(prefName, MODE_PRIVATE); 
      SharedPreferences.Editor editor = prefs.edit(); 
  
      //---save the values in the EditText view to preferences--- 
       editor.putString("TAG_KEY",value); 
       //---saves the values--- 
       editor.commit(); 
  }
  </code></pre>
  
  <p>}</p>
  
  <p><strong>Logcat:</strong></p>
  
  <pre><code>06-03 12:25:22.844: E/onCreate(29973): im Running
  06-03 12:25:23.174: E/Attendence(29973): Service Created
  06-03 12:25:30.702: E/onSTOP(29973): im calling...!!!!
  06-03 12:25:32.274: E/onCreate(29973): im Running
  06-03 12:25:33.655: E/onSTOP(29973): im calling...!!!!
  06-03 12:25:34.366: E/onCreate(29973): im Running
  06-03 12:25:35.878: E/onSTOP(29973): im calling...!!!!
  06-03 12:25:36.869: E/onRestart(29973): now im calling after onStop
  06-03 12:25:45.027: E/onSTOP(29973): im calling...!!!!
  </code></pre>
  
  <p><strong><code>06-03 12:25:48.221: E/Attendence(30447): Service Created</code></strong></p>
  
  <p>here in the logcat the last line shows that its call the onstartcommand method again. Why is it so? Even my Activity is not running I meant to say (the service starts in oncreate method on on acticity, but here in the logcat the control goes directly to the onStartCommand when i destroy my App ).</p>