继承IntentService

package com.example.service;
import android.content.Intent;
/**
 * 可以自动销毁的IntentService实例
 * @author Administrator
 *
 */
public class IntentService extends android.app.IntentService{
public IntentService() {
super("hello");
// TODO Auto-generated constructor stub
}
//IntentService内部集成了异步操作的线程类,该方法在一个单独的线程中执行
@Override
protected void onHandleIntent(Intent intent) {
for(int i=0;i<10;i++){
System.out.println(Thread.currentThread().getName()+"-"+i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
System.out.println("onDestroy()");
}


}

注意:

一:报错

7-30 10:55:15.887: E/libEGL(20608): cache file failed CRC check
07-30 10:55:19.585: E/AndroidRuntime(20608): FATAL EXCEPTION: main
07-30 10:55:19.585: E/AndroidRuntime(20608): Process: com.example.servicetest, PID: 20608
07-30 10:55:19.585: E/AndroidRuntime(20608): java.lang.RuntimeException: Unable to instantiate service com.example.servicetest.IntentServiceTest: java.lang.InstantiationException: class com.example.servicetest.IntentServiceTest has no zero argument constructor
07-30 10:55:19.585: E/AndroidRuntime(20608): at android.app.ActivityThread.handleCreateService(ActivityThread.java:2919)
07-30 10:55:19.585: E/AndroidRuntime(20608): at android.app.ActivityThread.access$1800(ActivityThread.java:176)
07-30 10:55:19.585: E/AndroidRuntime(20608): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1520)
07-30 10:55:19.585: E/AndroidRuntime(20608): at android.os.Handler.dispatchMessage(Handler.java:111)
07-30 10:55:19.585: E/AndroidRuntime(20608): at android.os.Looper.loop(Looper.java:194)
07-30 10:55:19.585: E/AndroidRuntime(20608): at android.app.ActivityThread.main(ActivityThread.java:5576)
07-30 10:55:19.585: E/AndroidRuntime(20608): at java.lang.reflect.Method.invoke(Native Method)
07-30 10:55:19.585: E/AndroidRuntime(20608): at java.lang.reflect.Method.invoke(Method.java:372)
07-30 10:55:19.585: E/AndroidRuntime(20608): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:955)
07-30 10:55:19.585: E/AndroidRuntime(20608): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:750)
07-30 10:55:19.585: E/AndroidRuntime(20608): Caused by: java.lang.InstantiationException: class com.example.servicetest.IntentServiceTest has no zero argument constructor
07-30 10:55:19.585: E/AndroidRuntime(20608): at java.lang.Class.newInstance(Class.java:1563)
07-30 10:55:19.585: E/AndroidRuntime(20608): at android.app.ActivityThread.handleCreateService(ActivityThread.java:2916)
07-30 10:55:19.585: E/AndroidRuntime(20608): ... 9 more
07-30 10:55:19.585: E/AndroidRuntime(20608): Caused by: java.lang.NoSuchMethodException: <init> []
07-30 10:55:19.585: E/AndroidRuntime(20608): at java.lang.Class.getConstructor(Class.java:531)
07-30 10:55:19.585: E/AndroidRuntime(20608): at java.lang.Class.getDeclaredConstructor(Class.java:510)
07-30 10:55:19.585: E/AndroidRuntime(20608): at java.lang.Class.newInstance(Class.java:1561)
07-30 10:55:19.585: E/AndroidRuntime(20608): ... 10 more
ntentServiece必须强制添加一个无参的构造方法,否则会报上述错误


二:

我们知道在Activiy中有两种方法跳转到指定的service中,一种是显式,一种是隐式。
1当我们使用隐式,例如我们使用action,配好了一service,
2,我们在Activity中
Intent intent=new Intent();
intent.setActio("使用配好的action");
startService(intent);
3,事实证明,这样会报错,提示通过action找到这个服务。
于是我便纠结在action是否配错。
最后百度,综合分析一下,在android5.0中不在支持这种隐式通过action来找到service了。





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值