Android RuntimeException: Unable to instantiate the service
Solution 1
In your concrete implementation you have to declare a default constructor which calls the public IntentService (String name)
super constructor of the abstract IntentService class you extend:
public MyService () {
super("MyServerOrWhatever");
}
You do not need to overwrite onStartCommand if the super implementation fits for you (what I expect).
In your current case you should get an exception (Unable to instantiate service...) - it is always worth to put this in the question.
Solution 2
Not the case here but this might help someone: Check that your service class is not abstract. I had this problem because I had copied IntentService implementation from SDK and modified it to better suit my needs.
Solution 3
I resolved the "Unable to instantiate the service" issue, by adding the default parameterless constructor.
Solution 4
This answer has been updated. Here is the updated, correct answer:
According to the documentation you do not have to override onStartCommand() for IntentServices, instead the documentation says the following about onStartCommand() for IntentServices: You should not override this method for your IntentService. Instead, override onHandleIntent(Intent), which the system calls when the IntentService receives a start request. (Thanks to Ready4Android).
Below is the original incorrect answer (left in so the comments make sense):
According to documentation you should override OnStartCommand() (or deprecated OnStart()) in order to process intent service start-up. Have you tried it? And as K. Claszen wrote - you need to implement default constructor.
Solution 5
ServiceDemo.java:
public class ServicesDemo extends Activity implements OnClickListener {
private static final String TAG = "ServicesDemo";
Button buttonStart, buttonStop;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
buttonStart = (Button) findViewById(R.id.buttonStart);
buttonStop = (Button) findViewById(R.id.buttonStop);
buttonStart.setOnClickListener(this);
buttonStop.setOnClickListener(this);
}
public void onClick(View src) {
switch (src.getId()) {
case R.id.buttonStart:
Log.w(TAG, "onClick: starting srvice");
startService(new Intent(this, MyService.class));
startActivity(new Intent(getApplicationContext(),Second.class));
break;
case R.id.buttonStop:
Log.w(TAG, "onClick: stopping srvice");
stopService(new Intent(this, MyService.class));
break;
}
}
}
MyService.java:
package com.example;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
public class MyService extends Service {
private static final String TAG = "MyService";
MediaPlayer player;
@Override
public IBinder onBind(Intent intent) {
Log.w(" ibinder ","");
return null;
}
@Override
public void onCreate() {
Toast.makeText(this, "My Service Created",0).show();
Log.w(TAG, "onCreate");
player = MediaPlayer.create(this,R.raw.frm7v1);
player.setLooping(true); // Set looping
}
@Override
public void onDestroy() {
Toast.makeText(this, "My Service Stopped",0).show();
Log.w(TAG, "onDestroy");
player.stop();
}
@Override
public void onStart(Intent intent, int startid) {
Toast.makeText(this, "My Service Started :"+intent+" start id :"+startid,0).show();
Log.d(TAG, "onStart");
player.start();
}
}
Declare the following attribute in manifest file:
<service android:enabled="true" android:name=".MyService" />
Related videos on Youtube
ram
Updated on January 17, 2020Comments
-
ram over 4 years
I want to create a service which will run on a separate thread (not on UI Thread), so I implemented a class which will extend IntentService. But I haven't got any luck. Here is the code.
public class MyService extends IntentService { public MyService(String name) { super(name); // TODO Auto-generated constructor stub } @Override public IBinder onBind(Intent arg0) { // TODO Auto-generated method stub return null; } @Override public void onCreate() { // TODO Auto-generated method stub super.onCreate(); Log.e("Service Example", "Service Started.. "); // pushBackground(); } @Override public void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); Log.e("Service Example", "Service Destroyed.. "); } @Override protected void onHandleIntent(Intent arg0) { // TODO Auto-generated method stub for (long i = 0; i <= 1000000; i++) { Log.e("Service Example", " " + i); try { Thread.sleep(700); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
Service Consumption in an Activity Button click:
public void onclick(View view) { Intent svc = new Intent(this, MyService.class); startService(svc); }
-
Peter Knego over 13 yearsTake a look at example: developer.android.com/reference/android/app/…
-
james about 13 yearsthe example you linked is of a
Service
not anIntentService
!!
-
-
Ready4Android about 13 yearsAccording to the documentation you dont have to override onStartCommand() for INTENTServices, instead the documentation says the following about onStartCommand() for IntentServices: You should not override this method for your IntentService. Instead, override onHandleIntent(Intent), which the system calls when the IntentService receives a start request.
-
Audrius about 13 years@Ready4Android You are right, I'll edit my answer so to not confuse others. Thanks.
-
MSpeed over 12 yearsThis is a good example of a Service, but not an IntentService as the question asked.
-
user2768 over 8 yearsI had the same problem, but I didn't notice this answer until I had debugged myself. @Juuso, I've edited your response to include some code, thereby making it more obvious.