美文网首页
IntentService详解

IntentService详解

作者: Skypew | 来源:发表于2017-12-04 16:28 被阅读7次

一 是什么

继承自service 优先级比service高 ,内部封装hander


image.png

IntentService最起码有两个好处,一方面不需要自己去new Thread了;另一方面不需要考虑在什么时候关闭该Service了。

二.使用方法

public class MainIntentServiceActivity extends AppCompatActivity implements MyService.UpdateUI {

    /**
     * 图片地址集合
     */
    private String url[] = {
            "http://pic6.huitu.com/res/20130116/84481_20130116142820494200_1.jpg",
            "http://sucai.qqjay.com/qqjayxiaowo/201210/26/1.jpg",
            "http://img02.tooopen.com/images/20140504/sy_60294738471.jpg",
            "http://img2.imgtn.bdimg.com/it/u=819201812,3553302270&fm=214&gp=0.jpg",
            "http://pic55.nipic.com/file/20141208/19462408_171130083000_2.jpg",
            "http://img05.tooopen.com/images/20150531/tooopen_sy_127457023651.jpg",
            "http://pic.58pic.com/58pic/17/41/38/88658PICNuP_1024.jpg"
    };


    private static ImageView imageView;


    //UI线程的hander
    private static final Handler mUIHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            imageView.setImageBitmap((Bitmap) msg.obj);
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_intent_service);


        imageView = (ImageView) findViewById(R.id.image);

        //只会调用 一次service实例  会顺序一个一个执行
        Intent intent = new Intent(this, MyService.class);
        for (int i = 0; i < 7; i++) {
            intent.putExtra(MyService.DOWNLOAD_URL, url[i]);
            intent.putExtra(MyService.INDEX_FLAG, i);
            startService(intent);
        }
        MyService.setUpdateUI(this);

    }

    //接口回调
    @Override
    public void updateUI(Message message) {
        //通知UI线程 加载 图片
        mUIHandler.sendMessageDelayed(message, message.what * 1000);
    }
}
public class MyService extends IntentService {


    public static final String DOWNLOAD_URL = "download_url";
    public static final String INDEX_FLAG = "index_flag";
    public static UpdateUI updateUI;


    //用来接口回调
    public static void setUpdateUI(UpdateUI updateUIInterface) {
        updateUI = updateUIInterface;
    }


    public MyService() {
        super("MyService");
    }



    /**
     * 实现异步任务的方法
     *
     * @param intent Activity传递过来的Intent,数据封装在intent中
     */
    @Override
    protected void onHandleIntent(@Nullable Intent intent) {

        //下载图片
        Bitmap bitmap = downloadUrlBitmap(intent.getStringExtra(DOWNLOAD_URL));

        Message msg1 = new Message();
        msg1.what = intent.getIntExtra(INDEX_FLAG, 0);
        msg1.obj = bitmap;
        if (updateUI != null) {
            updateUI.updateUI(msg1);
        }

    }


    /**
     * 网络下载 图片
     * @param urlString
     * @return
     */
    private Bitmap downloadUrlBitmap(String urlString) {
        HttpURLConnection urlConnection = null;
        BufferedInputStream in = null;
        Bitmap bitmap = null;
        try {
            final URL url = new URL(urlString);
            urlConnection = (HttpURLConnection) url.openConnection();
            in = new BufferedInputStream(urlConnection.getInputStream(), 8 * 1024);
            bitmap = BitmapFactory.decodeStream(in);
        } catch (final IOException e) {
            e.printStackTrace();
        } finally {
            if (urlConnection != null) {
                urlConnection.disconnect();
            }
            try {
                if (in != null) {
                    in.close();
                }
            } catch (final IOException e) {
                e.printStackTrace();
            }
        }
        return bitmap;
    }




    public interface UpdateUI {
        void updateUI(Message message);
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return super.onStartCommand(intent, flags, startId);

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public IBinder onBind(Intent intent) {
        return super.onBind(intent);
    }

}



三.源码解析

HandlerThread +hander 实现的


image.png image.png

相关文章

网友评论

      本文标题:IntentService详解

      本文链接:https://www.haomeiwen.com/subject/bbjxixtx.html