user3829999
user3829999

Reputation:

How to use Service in android

I'm new in android. I want to Use some background process. So I decided to use service. Therefore I make a simple app to learn using. But app Stops. In manifest I defined service. In log cat shows:

07-12 17:49:03.067: E/AndroidRuntime(3367): Caused by: java.lang.InstantiationException: can't instantiate class com.example.servicetest.service; no empty constructor

public class MainActivity extends Activity {
private Button b;
private TextView tv;
private BroadcastReceiver br=new BroadcastReceiver() {

    @Override
    public void onReceive(Context arg0, Intent arg1) {
        // TODO Auto-generated method stub
        tv.setText(arg1.getExtras().getString("s").toString());
    }
};
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    b=(Button) findViewById(R.id.button1);  
    tv=(TextView) findViewById(R.id.textView1);     
    b.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            startService(new Intent(getApplicationContext(), service.class));
        }
    });
}

public class service extends IntentService {
public service(String name) {
    super(name);
    // TODO Auto-generated constructor stub
}
@Override
protected void onHandleIntent(Intent intent) {
    // TODO Auto-generated method stu
    intent.putExtra("s", "salam");
    sendBroadcast(intent);
    }
}

Upvotes: 1

Views: 225

Answers (2)

Andrei Catinean
Andrei Catinean

Reputation: 883

The exception explicitly says : no empty constructor

You should add an empty constructor to your service. You should extract your service into a separate class and add a default public constructor to it:

public service() {
    super("MyService");
}

Additionally, you should register your BroadcastReceiver in onResume :

registerReceiver(br,new IntentFilter("event"));

And unregister it in onPause:

unregisterReceiver(br);

Now in your service in onHandleIntent send the broadcast:

Intent send = new Intent("event");
send.putExtra("s", "salam");
sendBroadcast(send);

Upvotes: 1

Yaroslav Mytkalyk
Yaroslav Mytkalyk

Reputation: 17115

Any Context descendant (Activity, Service, Application) bust be defined in it's own file. I'm not sure though if you can define it as a public static inner class, but it's a bad practice anyway.

So in order to make it work, extract it to a java file, like service.java and define it in the manifest

<service android:name="com.example.servicetest.service"/>

Don't use android:procces attrubute unless you have to by design because it adds too much complexity.

Consider that by java conventions, the class names start with uppercase letters.

Also, as Andrei Catinean mentioned, you have to define an empty constructor.

public service() {
    super("Your service name");
}

Upvotes: 0

Related Questions