Вопрос

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);
}
Это было полезно?

Решение

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.

Другие советы

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.

I resolved the "Unable to instantiate the service" issue, by adding the default parameterless constructor.

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" />

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.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top