title: Android Service not Register
body: <p>I am new to android. I want to build a app which start a service when the first activity start. And that service want to stay when I leave the main activity. Because the next Activity use that service to do some operation. </p>
  
  <p>So i write some code for that. (But i didn't write process for the second activity).
  When i click next button (to go to next activity) second activity launch and get Not responding message.</p>
  
  <p>An logcast says.</p>
  
  <pre><code>Unable to stop activity {com.example.autocomplete.app/com.example.autocomplete.app.MyActivity}: java.lang.IllegalArgumentException: Service not registered: com.example.autocomplete.app.MyActivity$1@534506f8
  </code></pre>
  
  <p>Please some one help to correct this error.
  Is there any way which we can do the above scenario except this way.</p>
  
  <p>So my Code is shown in below</p>
  
  <pre><code>package com.example.autocomplete.app;
  import android.app.Activity;
  import android.content.ComponentName;
  import android.content.Intent;
  import android.content.ServiceConnection;
  import android.os.Bundle;
  import android.os.IBinder;
  import android.view.Menu;
  import android.view.MenuItem;
  import android.view.View;
  import android.widget.Button;
  import android.widget.EditText;
  
  
  public class MyActivity extends Activity {
  EditText txtA;
  EditText txtB;
  EditText txtResult;
  Button btnAns;
  MySumService mservice;
  boolean mbound;
  ServiceConnection mcontn=new ServiceConnection() {
      @Override
      public void onServiceConnected(ComponentName componentName, IBinder service) {
          mbound=true;
          MySumService.LocalBinder binder=(MySumService.LocalBinder)service;
          mservice=binder.getService();
      }
  
      @Override
      public void onServiceDisconnected(ComponentName componentName) {
          mbound=false;
          mservice=null;
      }
  };
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_my);
      txtA=(EditText)findViewById(R.id.txtA);
      txtB=(EditText)findViewById(R.id.txtB);
      txtResult=(EditText)findViewById(R.id.txtResult);
      Button btnans=(Button)findViewById(R.id.button);
      btnans.setOnClickListener(new View.OnClickListener() {
  
          @Override
          public void onClick(View view) {
              int a=Integer.parseInt(txtA.getText().toString());
              int b=Integer.parseInt(txtB.getText().toString());
              int r=mservice.Sum(a,b);
              txtResult.setText(new String().valueOf(r));
  
          }
      });
      Button btnx=(Button)findViewById(R.id.button2);
      btnx.setOnClickListener(new View.OnClickListener(){
  
          @Override
          public void onClick(View view) {
              Intent p=new Intent(MyActivity.this,next.class);
              startActivity(p);
  
          }
      });
      Intent i=new Intent(this,MySumService.class);
      startService(i);
  }
  
  @Override
  protected void onDestroy() {
      super.onDestroy();
      if (mbound){
          mservice.unbindService(mcontn);
          mbound=false;
      }
  
  }
  
  @Override
  protected void onStart() {
      super.onStart();
      Intent i=new Intent(this,MySumService.class);
      bindService(i,mcontn,BIND_AUTO_CREATE);
  }
  
  @Override
  protected void onStop() {
      super.onStop();
      if (mbound){
          mservice.unbindService(mcontn);
          mbound=false;
      }
  }
  
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.my, menu);
      return true;
  }
  
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
      // Handle action bar item clicks here. The action bar will
      // automatically handle clicks on the Home/Up button, so long
      // as you specify a parent activity in AndroidManifest.xml.
      int id = item.getItemId();
      if (id == R.id.action_settings) {
          return true;
      }
      return super.onOptionsItemSelected(item);
  }
  }
  </code></pre>
  
  <p>MySumService.java</p>
  
  <p>package com.example.autocomplete.app;</p>
  
  <pre><code>import android.app.Service;
  import android.content.Intent;
  import android.content.IntentFilter;
  import android.os.Binder;
  import android.os.IBinder;
  import android.widget.Toast;
  
  /**
   * Created by NRV on 9/6/2014.
   */
  public class MySumService extends Service {
  private IBinder mBinder=new LocalBinder();
  int k=0;
  
  @Override
  public void onCreate() {
  
      super.onCreate();
  
  
  }
  
  @Override
  public IBinder onBind(Intent intent) {
      return mBinder;
  }
  
  public int Sum(int a, int b){
      return k;
  }
  
  public class LocalBinder extends Binder{
      public MySumService getService(){
          return MySumService.this;
      }
  }
  
  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
      for (int ip=0;ip&lt;1000;ip++){
          k=ip;
      }
      Toast.makeText(getApplicationContext(),"Online",Toast.LENGTH_SHORT).show();
      return super.onStartCommand(intent, flags, startId);
  }
  }
  </code></pre>
  
  <p>activity_my.xml</p>
  
  <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:paddingLeft="@dimen/activity_horizontal_margin"
  android:paddingRight="@dimen/activity_horizontal_margin"
  android:paddingTop="@dimen/activity_vertical_margin"
  android:paddingBottom="@dimen/activity_vertical_margin"
  tools:context=".MyActivity"&gt;
  
  &lt;TextView
      android:text="@string/hello_world"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/textView2" /&gt;
  
  &lt;TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:textAppearance="?android:attr/textAppearanceLarge"
      android:text="Text 1"
      android:id="@+id/textView"
      android:layout_below="@+id/textView2"
      android:layout_alignParentLeft="true"
      android:layout_marginTop="58dp" /&gt;
  
  &lt;TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:textAppearance="?android:attr/textAppearanceLarge"
      android:text="Text2"
      android:id="@+id/textView3"
      android:layout_centerVertical="true"
      android:layout_alignParentLeft="true" /&gt;
  
  &lt;EditText
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:id="@+id/txtA"
      android:layout_below="@+id/textView"
      android:layout_alignParentLeft="true" /&gt;
  
  &lt;EditText
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:id="@+id/txtB"
      android:layout_below="@+id/textView3"
      android:layout_alignParentLeft="true" /&gt;
  
  &lt;EditText
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:id="@+id/txtResult"
      android:layout_below="@+id/txtB"
      android:layout_alignParentLeft="true"
      android:layout_marginTop="42dp" /&gt;
  
  &lt;Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="New Button"
      android:id="@+id/button"
      android:layout_alignParentBottom="true"
      android:layout_toRightOf="@+id/textView3" /&gt;
  
  &lt;Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="----&gt;"
      android:id="@+id/button2"
      android:layout_below="@+id/txtResult"
      android:layout_toRightOf="@+id/textView2" /&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>lout2.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"&gt;
  
  &lt;Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="New Button"
      android:id="@+id/button" /&gt;
  
  &lt;TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:textAppearance="?android:attr/textAppearanceSmall"
      android:text="Small Text"
      android:id="@+id/textView" /&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>next.java</p>
  
  <pre><code>package com.example.autocomplete.app;
  
  import android.app.Activity;
  import android.os.Bundle;
  
  /**
   * Created by NRV on 9/6/2014.
   */
  public class next extends Activity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.lout2);
  }
  }
  </code></pre>