48

我想创建一个将在单独的线程(而不是 UI 线程)上运行的服务,所以我实现了一个扩展 IntentService 的类。但我没有运气。这是代码。

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();
            }
        }
    }
}

Activity 按钮中的服务消费点击:

public void onclick(View view) {
Intent svc = new Intent(this, MyService.class);
    startService(svc);
}
4

5 回答 5

68

在您的具体实现中,您必须声明一个默认构造函数,该构造函数调用public IntentService (String name)您扩展的抽象 IntentService 类的超级构造函数:

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

如果超级实现适合您(我所期望的),您不需要覆盖 onStartCommand 。

在您当前的情况下,您应该得到一个异常(无法实例化服务......) - 总是值得把它放在问题中。

于 2011-02-17T10:31:35.710 回答
4

这里不是这种情况,但这可能会对某人有所帮助:检查您的服务类是否不是 abstract。我遇到了这个问题,因为我从 SDK 复制了 IntentService 实现并对其进行了修改以更好地满足我的需求。

于 2013-12-02T17:25:45.100 回答
1

此答案已更新。这是更新的正确答案:

根据文档,您不必为 IntentServices 重写 onStartCommand(),而是文档对 IntentServices 的 onStartCommand() 进行了以下说明:您不应该为您的 IntentService 重写此方法。相反,重写 onHandleIntent(Intent),当 IntentService 接收到启动请求时系统调用它。(感谢 Ready4Android)。


以下是原始的错误答案(保留在其中,因此评论有意义):

根据文档,您应该覆盖 OnStartCommand()(或不推荐使用的 OnStart())以处理意图服务启动。你试过了吗?正如K. Claszen所写 - 您需要实现默认构造函数。

于 2011-02-17T10:31:52.257 回答
1

服务演示.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;
    }
  }
}

我的服务.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();
    }
}

在清单文件中声明以下属性:

  <service android:enabled="true" android:name=".MyService" />
于 2011-10-04T06:45:25.763 回答
1

我通过添加默认的无参数构造函数解决了“无法实例化服务”问题。

于 2012-07-01T14:17:41.393 回答