我想创建一个服务,它将在一个单独的线程上运行(而不是在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();
}
}
}
}
活动按钮中的服务消耗单击:
public void onclick(View view) {
Intent svc = new Intent(this, MyService.class);
startService(svc);
}
答案 0 :(得分:65)
在具体实现中,您必须声明一个默认构造函数,该构造函数调用您扩展的抽象IntentService类的public IntentService (String name)
超级构造函数:
public MyService () {
super("MyServerOrWhatever");
}
如果超级实现适合你(我期望的话),你不需要覆盖onStartCommand。
在你目前的情况下,你应该得到一个例外(无法实例化服务......) - 总是值得把这个放在问题中。
答案 1 :(得分:2)
不是这里的情况,但这可能有助于某人: 检查您的服务类是否为抽象。我遇到了这个问题,因为我从SDK复制了IntentService实现并对其进行了修改以更好地满足我的需求。
答案 2 :(得分:1)
根据文档,您不必为IntentServices覆盖onStartCommand(),而是文档说明了关于IntentServices的onStartCommand()的以下内容:您不应该为IntentService重写此方法。相反,覆盖onHandleIntent(Intent),系统在IntentService收到启动请求时调用。 (感谢Ready4Android)。
以下是原始错误答案(左边是注释有意义):
根据documentation,您应该重写OnStartCommand()(或弃用的OnStart())以处理意图服务启动。你试过吗?正如K. Claszen所写 - 您需要实现默认构造函数。
答案 3 :(得分:1)
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" />
答案 4 :(得分:1)
我通过添加默认的无参数构造函数来解决“无法实例化服务”问题。