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.
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.
An logcast says.
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
Please some one help to correct this error. Is there any way which we can do the above scenario except this way.
So my Code is shown in below
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);
}
}
MySumService.java
package com.example.autocomplete.app;
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<1000;ip++){
k=ip;
}
Toast.makeText(getApplicationContext(),"Online",Toast.LENGTH_SHORT).show();
return super.onStartCommand(intent, flags, startId);
}
}
activity_my.xml
<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">
<TextView
android:text="@string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView2" />
<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" />
<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" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/txtA"
android:layout_below="@+id/textView"
android:layout_alignParentLeft="true" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/txtB"
android:layout_below="@+id/textView3"
android:layout_alignParentLeft="true" />
<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" />
<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" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="---->"
android:id="@+id/button2"
android:layout_below="@+id/txtResult"
android:layout_toRightOf="@+id/textView2" />
</RelativeLayout>
lout2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Small Text"
android:id="@+id/textView" />
</LinearLayout>
next.java
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);
}
}
If you want your service to stay running until explicitly stopped, you need to START_STICKY
in your onStartCommand()
:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
handleCommand(intent);
// We want this service to continue running until it is explicitly
// stopped, so return sticky.
return START_STICKY;
}
That way, it will persist across activities.
Now for the exception:
java.lang.IllegalArgumentException: Service not registered
means that you weren't bound to service during unbindService()
call.