错误:can’t instantiate class IntentService no empty constructor

     IntentService 服务的原理,就是启动一个服务,然后开启一个工作线程,当工作线程执行完后,服务就会自动销毁,这种方式可以有效的运行超大时间的工作代码,非常有用,但是在编写的时候出现了一个错误,错误的原因就是没有在super.("这里")指定一个服务名.
public class TestIntentService extends IntentService {
 private static final String TAG = "TestIntentService";
 /**
 * Creates an IntentService. Invoked by your subclass's constructor.
 */
 public TestIntentService() {
 super("TestIntentService");//原来是需要指定一个服务名称
 }

QQ截图20160605142225

Logcat的日志:

06-05 02:21:53.285 21618-21618/com.lanxin.testreceiver I/TestReceiver: before3
06-05 02:21:53.305 21618-21618/com.lanxin.testreceiver I/TestReceiver: after:3
06-05 02:21:53.475 21618-21618/com.lanxin.testreceiver D/TestReceiver1: main:(id)1:(priority)5:(group)main
06-05 02:21:53.475 21618-21618/com.lanxin.testreceiver I/TestReceiver1: 测试一条消息:2
06-05 02:21:53.575 21618-21618/com.lanxin.testreceiver D/TestReceiver1: main:(id)1:(priority)5:(group)main
06-05 02:21:53.595 21618-21618/com.lanxin.testreceiver D/TestReceiver2: main:(id)1:(priority)5:(group)main
06-05 02:21:55.595 21618-21618/com.lanxin.testreceiver I/TestReceiver2: 测试一条消息:2
06-05 02:21:55.595 21618-21618/com.lanxin.testreceiver D/TestReceiver2: main:(id)1:(priority)5:(group)main
06-05 02:21:55.715 21618-21618/com.lanxin.testreceiver I/TestIntentService: Service onCreate
06-05 02:21:55.715 21618-21618/com.lanxin.testreceiver I/TestIntentService: Service onStartCommand
06-05 02:21:55.745 21618-22366/com.lanxin.testreceiver D/TestIntentService: IntentService[TestIntentService]:(id)109:(priority)5:(group)main
06-05 02:21:55.745 21618-22366/com.lanxin.testreceiver I/TestIntentService: onHandleIntent:0
06-05 02:21:55.755 21618-22366/com.lanxin.testreceiver D/TestIntentService: IntentService[TestIntentService]:(id)109:(priority)5:(group)main
06-05 02:21:55.755 21618-22366/com.lanxin.testreceiver I/TestIntentService: onHandleIntent:1
06-05 02:21:56.015 21618-21618/com.lanxin.testreceiver I/TestIntentService: Service onDestroy

 

android开发:AsyncTask深入解析

AsyncTask介绍
Android的AsyncTask比Handler更轻量级一些,适用于简单的异步处理。
首先明确Android之所以有Handler和AsyncTask,都是为了不阻塞主线程(UI线程),且UI的更新只能在主线程中完成,因此异步处理是不可避免的。
Android为了降低这个开发难度,提供了AsyncTask。AsyncTask就是一个封装过的后台任务类,顾名思义就是异步任务。

16263646566114
 
Copyright © 2008-2021 lanxinbase.com Rights Reserved. | 粤ICP备14086738号-3 |