您的位置:首页 > 移动开发 > Android开发

Android开发:IntentService使用(源码分析)

2015-06-25 16:38 302 查看
IntentService继承自Service,那么它与Service又有哪些不同?使用起来有哪些需要注意的地方呢?什么时候使用Service,什么时候使用IntentService?一起来看看。

转载请注明作者xiong_it和本文链接:/article/2590643.html,谢谢!

IntentService与Service的区别

使用intentService与service有什么不同呢:
(1)IntentService直接创建一个默认的工作线程(子线程)无需担心线程阻塞问题发生,Service默认在主线程(UI线程)执行;
(2)IntentService直接创建一个工作队列,将意图传递给你onHandleIntent()的实现,并且一次只处理一个意图,Service默认将意图传递给onStartCommand()处理;
(3)IntentService当请求完成后自己会调用stopSelf(),所以你就不用调用该方法了,Service需要手动调用stopSelf来终止服务(前提是使用startService()开启服务)。
下面,我们将通过源码来认识IntentService。


IntentService源码分析

@Override
    public void onCreate() {
        // TODO: It would be nice to have an option to hold a partial wakelock
        // during processing, and to have a static startService(Context, Intent)
        // method that would launch the service & hand off a wakelock.

        super.onCreate();
        HandlerThread thread = new HandlerThread("IntentService[" + mName + "]");
        thread.start();

        mServiceLooper = thread.getLooper();
        mServiceHandler = new ServiceHandler(mServiceLooper);
    }
从IntentService的onCreate()可以看出,在IntentService内部,给我们创建了一个HandlerThread的工作线程,并且得到一个looper对象,用于消息队列传输,并且由mServiceHandler处理相应消息。正好验证了IntentService与Service的区别的(1)(2)两点。有的朋友可能会问了,Looper一般情况下,只能在主线程使用吗?是的,一般情况下,Looper只在主线程中使用,但是HandlerThread继承自Thread,重写了Thread的run()方法:
@Override
    public void run() {
        mTid = Process.myTid();
        Looper.prepare();
        synchronized (this) {
            mLooper = Looper.myLooper();
            notifyAll();
        }
        Process.setThreadPriority(mPriority);
        onLooperPrepared();
        Looper.loop();
        mTid = -1;
    }
在子线程中使用Looper时,需要使用Looper.prepare(),和Looper.loop()来使得当前线程(也就是子线程)作为Looper的使用场所,并传递消息队列。

IntentService的OnStartCommand()源码
/**
     * You should not override this method for your IntentService. Instead,
     * override {@link #onHandleIntent}, which the system calls when the IntentService
     * receives a start request.
     * @see android.app.Service#onStartCommand
     */
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        onStart(intent, startId);
        return mRedelivery ? START_REDELIVER_INTENT : START_NOT_STICKY;
    }
指出:在IntentService中,无需实现onStartCommand(),我们需要做的就是实现onHandlerIntent()方法(You should not override this method for your IntentService. Instead override {@link #onHandleIntent}, which the system calls when the IntentService)。

在IntentService中,当请求完成后,mServiceHandler会收到消息,它会自动调用stopSelf()终止自身服务:
private final class ServiceHandler extends Handler {
        public ServiceHandler(Looper looper) {
            super(looper);
        }

        @Override
        public void handleMessage(Message msg) {
            onHandleIntent((Intent)msg.obj);
            stopSelf(msg.arg1);
        }
    }
这也验证了上面的区别(3),而Service是做不到这一点的,我们需要手动调用stopSelf来自杀。

IntentService的使用注意事项

IntentService构造方法的问题
每个类的构造函数都是必需的,在我们继承IntentService时,会提示实现一个构造函数,使用Eclipse自动生成的构造函数是这样的:
/**
	 * @param name
	 */
	public MyService(String name) {
		super(name);
		// TODO Auto-generated constructor stub
	}
但是运行的时候,开启该服务会崩溃,并报出错误:
java.lang.InstantiationException: can't instantiate class : no empty constructor
意思是说,需要一个空的构造方法,于是,给MyService加上一个无参数的空构造方法:
public MyService() {
		
	}
但是Eclipse红色警告:
Implicit super constructor IntentService() is undefined. Must explicitly invoke another constructor

这就很奇怪了:IntentService在没有空的构造方法运行崩溃,加上又警告提示,IntentService中没有定义这样的构造方法。解决办法是:
public MyService() {
		super(SERVICE_NAME);//SERVICE_NAME是自定义的字符串
	}


何时使用IntentService

相信看完,大家应该对什么时候使用IntentService有数了,只要了解了IntentService和Service的区别,还是很好把我的,希望本文对大家有所帮助。

转载请注明作者xiong_it和本文链接:/article/2590643.html,谢谢!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: