Android RuntimeException:无法实例化服务

我想创build一个服务,将运行在一个单独的线程(而不是在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(); } } } } 

活动button中的服务消耗点击:

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

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

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

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

在你目前的情况下,你应该得到一个exception(无法实例化服务…) – 总是值得把这个问题。

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

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

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

  <service android:enabled="true" android:name=".MyService" /> 

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

根据文档,您不必为IntentServices重写onStartCommand(),而是为IntentServices提供以下关于onStartCommand()的说明:您不应该为IntentService重写此方法。 相反,重写onHandleIntent(Intent),当IntentService接收到一个开始请求时,系统调用它。 (感谢Ready4Android)。


下面是原来不正确的答案(留在评论意义上):

根据文档,您应该重写OnStartCommand()(或已弃用的OnStart())以处理意向服务启动。 你试过了吗? 正如K. Claszen写的 – 你需要实现默认的构造函数。

不是这里的情况,但这可能有助于某人:检查你的服务类是不是抽象的 。 我有这个问题,因为我从SDK复制了IntentService实现,并进行了修改,以更好地满足我的需求。