在即时聊天中可能会存在一个隐藏的Bug,这个Bug根据手机的网速和性能有关系,比如你即时聊天中,你发送一消息,你的网络情况不是很好,这个时候你发送的消息一直处于发送状态,然后你不想看了,就按退出,这个时候Activity或者Fragment被销毁的时候就导致了这个消息被强行GC了,所以为了解决这个方案,我们可以使用IntentService,什么是IntentService?
从这个解释中可以看出来是一个异步服务,而且不用担心他自己的生命周期.所以我们就可以使用它去发送消息,当然消息发送完毕后,我们肯定要通知界面更新UI,这个时候我们就需要使用广播比较方便些.我们可以这样写一个IntentService:
- package com.softtanck.intentservicedemo.service;
-
- import android.app.IntentService;
- import android.content.Context;
- import android.content.Intent;
-
- import com.softtanck.intentservicedemo.MainActivity;
-
-
-
-
- public class UpLoadImgService extends IntentService {
-
-
- public UpLoadImgService() {
- super("ceshi");
- }
-
-
-
-
-
-
- public UpLoadImgService(String name) {
- super(name);
- }
-
-
- public static void startUploadImg(Context context, String path) {
- Intent intent = new Intent(context, UpLoadImgService.class);
- intent.setAction(MainActivity.UPLOAD_IMG);
- intent.putExtra(MainActivity.EXTRA_IMG_PATH, path);
- context.startService(intent);
- }
-
-
- @Override
- protected void onHandleIntent(Intent intent) {
-
- if (null != intent) {
- String action = intent.getAction();
-
- if (action.equals(MainActivity.UPLOAD_IMG)) {
-
- uploadImg(intent.getStringExtra(MainActivity.EXTRA_IMG_PATH));
- }
- }
-
- }
-
- private void uploadImg(String path) {
- try {
- Thread.sleep(2000);
- Intent intent = new Intent(MainActivity.UPLOAD_IMG);
- intent.putExtra(MainActivity.EXTRA_IMG_PATH, path);
- sendBroadcast(intent);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
然后在需要的地方去调用:
- UpLoadImgService.startUploadImg(MainActivity.this, "/sdcard/cache/com.softtanck.intentservice/1.png");
还有就是IntentService是继承的Service,那么它是怎么实现异步线程的.?我们先粗略看一下它的源码:
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- package android.app;
-
- import android.annotation.WorkerThread;
- import android.content.Intent;
- import android.os.Handler;
- import android.os.HandlerThread;
- import android.os.IBinder;
- import android.os.Looper;
- import android.os.Message;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public abstract class IntentService extends Service {
- private volatile Looper mServiceLooper;
- private volatile ServiceHandler mServiceHandler;
- private String mName;
- private boolean mRedelivery;
-
- 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);
- }
- }
-
-
-
-
-
-
- public IntentService(String name) {
- super();
- mName = name;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public void setIntentRedelivery(boolean enabled) {
- mRedelivery = enabled;
- }
-
- @Override
- public void onCreate() {
-
-
-
-
- super.onCreate();
- HandlerThread thread = new HandlerThread("IntentService[" + mName + "]");
- thread.start();
-
- mServiceLooper = thread.getLooper();
- mServiceHandler = new ServiceHandler(mServiceLooper);
- }
-
- @Override
- public void onStart(Intent intent, int startId) {
- Message msg = mServiceHandler.obtainMessage();
- msg.arg1 = startId;
- msg.obj = intent;
- mServiceHandler.sendMessage(msg);
- }
-
-
-
-
-
-
-
- @Override
- public int onStartCommand(Intent intent, int flags, int startId) {
- onStart(intent, startId);
- return mRedelivery ? START_REDELIVER_INTENT : START_NOT_STICKY;
- }
-
- @Override
- public void onDestroy() {
- mServiceLooper.quit();
- }
-
-
-
-
-
-
- @Override
- public IBinder onBind(Intent intent) {
- return null;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
- @WorkerThread
- protected abstract void onHandleIntent(Intent intent);
- }
从源码中可以看出在OnCreat的时候初始化了一个HandlerThread,然后通过Looper的Loop去从消息队列里面去,建立了Handler的通信,而HandlerMessage中调用一个抽象的方法就是我们继承IntentService中的要实现的方法,该方法就是在线程中的,所以不需要再去开启线程.它的生命周期也是由Service是管理的.