IntentService教给自己如何葡京在线开户

总结

主要学习到了:

  1. 可以运用Handler去尊崇一个队列,很简短
  2. HandlerThread类
  3. Handler的handleMessage方法所在的线程决定于它的Looper所在的线程

IntentService代码虽少,可是非凡好.
除此以外,Android还有为数不少很好的类需求自身去发现,去学习!~
又学了一招,哈哈!~

其余欢迎关心:
我的Github
本身的虎扑
自我的微信公众号:

微信公众号

(以下仅表示个人观感,仅供参考,软件亲测)

一探讨竟

IntentService源码不多,截取了有的:

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
            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();
        //getLooper 来获取thread的looper
        mServiceLooper = thread.getLooper();
        //拿looper实例化 handler
        mServiceHandler = new ServiceHandler(mServiceLooper);
    }

    @Override
    public void onStart(Intent intent, int startId) {
        Message msg = mServiceHandler.obtainMessage();
        msg.arg1 = startId;
        msg.obj = intent;
        //封装intent的请求 用handler发送
        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();
    }

    protected abstract void onHandleIntent(Intent intent);
}

成员变量不多,一个looper,一个handler,接下去分析一下:
在onCreate里,实例化一个HandlerThread(继承自Thread,HandlerThread后边再说)并且启动线程,并且选拔该HandlerThread线程的looper实例化handler.
onStart里接受的intent,封装成Message用handler发送,并在handleMessage中调用onHandleIntent((Intent)msg.obj);,再stopSelf.
另外在onDestroy里调用了looper.quit();退出looper.

诸如此类intentService的体制原理也大概看完了,其实简单,不过也能学到不少事物:

  1. HandlerThread类–>官方资料

Handy class for starting a new thread that has a looper. The looper
can then be used to create handler classes. Note that start() must
still be called.

惠及大家创设一个持有looper的线程,可以用来创立handler.注意: start()
必须被调用!

先前也不亮堂这些类,本次索性来看看HandlerThread的源码:

public class HandlerThread extends Thread {
    int mPriority;
    int mTid = -1;
    Looper mLooper;

    public HandlerThread(String name) {
        super(name);
        mPriority = Process.THREAD_PRIORITY_DEFAULT;
    }

    public HandlerThread(String name, int priority) {
        super(name);
        mPriority = priority;
    }

    protected void onLooperPrepared() {
    }

    @Override
    public void run() {
        mTid = Process.myTid();
        Looper.prepare();
        synchronized (this) {
            mLooper = Looper.myLooper();
            notifyAll();
        }
        Process.setThreadPriority(mPriority);
        onLooperPrepared();
        Looper.loop();
        mTid = -1;
    }

    /**
     * This method returns the Looper associated with this thread. If this thread not been started
     * or for any reason is isAlive() returns false, this method will return null. If this thread
     * has been started, this method will block until the looper has been initialized.  
     * @return The looper.
     */
    public Looper getLooper() {
        if (!isAlive()) {
            return null;
        }

        // If the thread has been started, wait until the looper has been created.
        synchronized (this) {
            while (isAlive() && mLooper == null) {
                try {
                    wait();
                } catch (InterruptedException e) {
                }
            }
        }
        return mLooper;
    }

    public boolean quit() {
        Looper looper = getLooper();
        if (looper != null) {
            looper.quit();
            return true;
        }
        return false;
    }

    public boolean quitSafely() {
        Looper looper = getLooper();
        if (looper != null) {
            looper.quitSafely();
            return true;
        }
        return false;
    }

    public int getThreadId() {
        return mTid;
    }
}

我们都知晓在线程里实例化handler要调用Looper.prepare();Looper.loop();,其实就是因为Looper
而HandlerThread有个分子变量mLooper,并且在run里面实例化了它,并且已经为大家封装好了Looper相关的点子~

注意getLooper办法,当mLooper为null时会向来wait,直到run措施里实例化mLooper并notifyAll,可以学学一下.

用法就拿一下IntentService里的代码吧:

HandlerThread thread = new HandlerThread("IntentService[" + mName + "]");
thread.start();
mServiceLooper = thread.getLooper();
mServiceHandler = new ServiceHandler(mServiceLooper);
  1. Handler类的handleMessage措施所在的线程决定于它的Looper所在的线程,那几个在此之前一贯没留意.

3、程序员效用工具 — updated in 2017-6-4 by 吕浪

引用自Quora问答:
  What are the best productivity tools for
programmers?

Answer Wiki:
1.Lots of RAM / Fast Internet / SSDs
2.Good headphones(一副好耳麦)
3.Scheduled meetings
4.A good issue tracker (+reporting tools for your team members:
Usersnap to get all necessary meta infos and a screenshot
automatically for every reported bug)
5.Distributed version control (Git, Mercurial (software), Darcs)
6.Google / Stack overflow / Quora
7.Pen and Paper
8.Count down timer (focusalot.com is a task planner and time tracking
app that helps programmers be more productive)
9.Dropbox
10.IDEs
11.Code generation tools
12.Dual screens(双显示器)
13.ProofHub
14.Workflowy as an Outliner
15.Orgzit – Orgzit – Organize, Analyze, and Collaborate on any
business related information

Retina macbook pro高配版


IntentService

明天学习了瑟维斯,那边是笔记,是时候来读书一下IntentService了.

反之亦然,先看下官网介绍:

IntentService is a base class for Services that handle asynchronous
requests (expressed as Intents) on demand. Clients send requests
through startService(Intent) calls; the service is started as needed,
handles each Intent in turn using a worker thread, and stops itself
when it runs out of work.
This “work queue processor” pattern is commonly used to offload tasks
from an application’s main thread. The IntentService class exists to
simplify this pattern and take care of the mechanics. To use it,
extend IntentService and implement onHandleIntent(Intent).
IntentService will receive the Intents, launch a worker thread, and
stop the service as appropriate.
All requests are handled on a single worker thread — they may take as
long as necessary (and will not block the application’s main loop),
but only one request will be processed at a time.

简短统计一下:
service的子类,用于拍卖异步请求,并且在干活线程按顺序处理请求,工作完后自己甘休.
选择也很简短,继承IntentService,已毕onHandleIntent(Intent)措施即可.
特点:

  1. 与Service不同,IntentService是起线程处理任务的
  2. 自带队列,每个请求都会排队
  3. 任务到位自己会调用stopSelf完工,无需大家担心

4、网站、浏览器相关

检索:谷歌、谷歌学术搜索
  技术问答神器:stackOverflow(https://stackoverflow.com/
  代码托管:GitHub(https://github.com
  图片存储和CDN:七牛云
  NBA:NBA直播吧(https://www.zhibo8.cc/
  pdf在线转word:smallPDF(https://smallpdf.com/

and so on…

2、首推手机软件 — updated in 2017-6-4 by 吕浪

社交软件:QQ、微信、推特(Twitter)
  应用市场:APKPure
  科学上网:VPN-Master(APKPure下载)、cloudVPN
  音乐:天涯论坛云音乐
  地图:高德地图
  支付:支付宝
  资讯整合:Zaker新闻、简书、豆瓣、reddit(需科学上网)
  问答社区:博客园、Qoura
  记账:乐乎有钱
  输入法:搜狗输入法
  QQ安全:QQ安全基本
  云笔记:为知笔记
  电子邮件:MIUI自带、QQ邮箱
  K歌:全民K歌、唱吧
  浏览器:QQ浏览器
  便签:MIUI自带
  密码管理:keepass
  电脑文件远程共享:ES文件浏览器
  FM和听书:喜马拉雅电台、蜻蜓FM、凤凰FM


1、首推电脑软件 — updated in 2017-6-4 by 吕浪

压缩:7zip
  管家和杀毒:腾讯处理器管家
  社交软件:QQ、微信电脑版
  浏览器:chrome、360浏览器、firefox
  资源管理器:clover
  Git:git-bash
  命令行:git-bash、cmder
  代码编辑器:Sublime Text、gVim
  文本编辑器:Notepad++
  markdown编辑器:Haroopad、MarkdownPad
  office:MS office
  ftp:Filezilla
  文件搜索:Listary、everything
  邮件:Foxmail
  视频播放器:potplayer
  下载器:迅雷极速版
  网络视频下载:维棠FLV摄像下载
  音乐:网易云、酷狗、QQ音乐、foobar2000
  网盘:百度网盘
  驱动:驱动人生
  密码管理:keepass
  云笔记:为知笔记
  图片编辑:美图秀秀
  科学上网:XX-Net、shadowsocks
  远程连接:putty
  翻译词典:乐乎有道词典
  右下角弹窗拦截:CloseADs
  pdf阅读器:极速pdf
  编译器:python、mingw、java
  敲键盘声音:tickeys
  Windows和office激活:小马激活、网搜找到激活密钥
  输入法:搜狗输入法
  浏览器广告拦截插件:adblock(plus)
  wifi共享:猎豹wifi
  分区备份还原:onekey一键还原
  硬盘文件修复:DiskGenius(带硬盘分区等作用)
  微软官方纯净版软件下载(Windows/office等):“MSDN,我报告你”网站
  钉住窗口:window on top
  截屏:shareX、QQ自带、系统自带
  文库下载:冰点文库下载器
  刻录iso文件到U盘:软碟通(注意要无毒的)
  电脑使用提示:fadetop、腾讯电脑管家的施用时间提醒成效
  RSS阅读器:foxmail、MS outlook
  流程图:MS visio、processOn(在线)
  U盘装系统:软碟通(注意下无毒的)
  虚拟机:VirtualBox
  pdf转word:极速pdf转word、smallPDF(在线:https://smallpdf.com/