安卓的RuntimeException:无法实例化服务 [英] Android RuntimeException: Unable to instantiate the service
问题描述
我想创建一个将运行在一个单独的线程(不是UI线程)服务,让我实现了一个类,将延长IntentService。但是我还没有得到任何运气。这里是code。
公共类的MyService延伸IntentService {
公共则将MyService(字符串名称){
超(名称);
// TODO自动生成构造函数存根
}
@覆盖
公众的IBinder onBind(意向为arg0){
// TODO自动生成方法存根
返回null;
}
@覆盖
公共无效的onCreate(){
// TODO自动生成方法存根
super.onCreate();
Log.e(服务实例,服务启动。);
// pushBackground();
}
@覆盖
公共无效的onDestroy(){
// TODO自动生成方法存根
super.onDestroy();
Log.e(服务实例,服务被摧毁。);
}
@覆盖
保护无效onHandleIntent(意向为arg0){
// TODO自动生成方法存根
为(长I = 0; I< = 1000000;我++){
Log.e(服务实施例,+ I);
尝试 {
视频下载(700);
}赶上(InterruptedException异常E){
// TODO自动生成的catch块
e.printStackTrace();
}
}
}
}
服务消费的活动按钮点击:
公共无效的onclick(查看视图){
意图SVC =新的意图(这一点,MyService.class);
startService(SVC);
}
在您的具体实现,你必须声明默认构造函数调用公共IntentService(字符串名称)
抽象IntentService类的超级构造函数你扩展:
公共则将MyService(){
超级(MyServerOrWhatever);
}
您不必覆盖onStartCommand,如果你超级实施适合(我希望)。
在你目前的情况下,你应该得到一个例外(无法实例化服务...) - 它始终是值得把这个问题
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.
这篇关于安卓的RuntimeException:无法实例化服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!