android:name=“.MyIntentService”
android:exported=“false”>
- 3)启动我们的Service,这里使用context.startService(intent)启动,当然你也可以用bindService启动
Intent intent = new Intent(context, MyIntentService.class);
intent.setAction(ACTION_UPLOAD);
intent.putExtra(EXTRA_PARAM1, param1);
intent.putExtra(EXTRA_PARAM2, param2);
context.startService(intent);
测试代码如下
public void onButtonClick(View view) {
switch (view.getId()) {
case R.id.btn_download:
MyIntentService.startActionDownLoad(MainActivity.this, “下载”, “发起者主线程”);
break;
case R.id.btn_upload:
MyIntentService.startActionUpload(MainActivity.this, “上传”, “发起者主线程”);
break;
default:
break;
}
}
运行以上测试程序,依次点击模拟上传和模拟下载,将可在我们的控制台上看到以下的log信息
控制台输出的Log
IntentService源码分析
这里先贴出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((Intent)msg.obj);
stopSelf(msg.arg1);
}
}
/**
-
Creates an IntentService. Invoked by your subclass’s constructor.
-
@param name Used to name the worker thread, important only for debugging.
*/
public IntentService(String name) {
super();
mName = name;
}
/**
-
Sets intent redelivery preferences. Usually called from the constructor
-
with your preferred semantics.
-
If enabled is true,
-
{@link #onStartCommand(Intent, int, int)} will return
-
{@link Service#START_REDELIVER_INTENT}, so if this process dies before
-
{@link #onHandleIntent(Intent)} returns, the process will be restarted
-
and the intent redelivered. If multiple Intents have been sent, only
-
the most recent one is guaranteed to be redelivered.
-
If enabled is false (the default),
-
{@link #onStartCommand(Intent, int, int)} will return
-
{@link Service#START_NOT_STICKY}, and if the process dies, the Intent
-
dies along with it.
*/
public void setIntentRedelivery(boolean enabled) {
mRedelivery = enabled;
}
@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);
}
@Override
public void onStart(Intent intent, int startId) {
Message msg = mServiceHandler.obtainMessage();
msg.arg1 = startId;
msg.obj = intent;
mServiceHandler.sendMessage(msg);
}
/**
-
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;
}
@Override
public void onDestroy() {
mServiceLooper.quit();
}
/**
-
Unless you provide binding for your service, you don’t need to implement this
-
method, because the default implementation returns null.
-
@see android.app.Service#onBind
*/
@Override
public IBinder onBind(Intent intent) {
return null;
}
/**
-
This method is invoked on the worker thread with a request to process.
-
Only one Intent is processed at a time, but the processing happens on a
-
worker thread that runs independently from other application logic.
-
So, if this code takes a long time, it will hold up other requests to
-
the same IntentService, but it will not hold up anything else.
-
When all requests have been handled, the IntentService stops itself,
-
so you should not call {@link #stopSelf}.
-
@param intent The value passed to {@link
-
android.content.Context#startService(Intent)}.
*/
@WorkerThread
protected abstract void onHandleIntent(Intent intent);
}
源码分析
分析之前我们先来看一下Service生命周期
这里我们以startService()为例分析,从我们启动一个IntentService,调用的过程大概是这样的,
构造方法 ->onCreate()- >onStartCommand()- >onStart()->Service running–>
- 1)首先我们先来看一下构造方法里面做了什么事情
public IntentService(String name) {
super();
mName = name;
}
其实很简单,只是调用父类的构造方法,并保存我们的那么字段
- 2)接着我们来看我们的onCreate方法做了什么?
@Override
public void onCreate() {
super.onCreate();
HandlerThread thread = new HandlerThread(“IntentService[” + mName + “]”);
thread.start();
mServiceLooper = thread.getLooper();
mServiceHandler = new ServiceHandler(mServiceLooper);
}
简单来说,就是为我们初始化一个线程thread并启动它,并将线程的looper与我们的mServiceHandler绑定在一起。
- 3)接着我们来看onStartCommand()方法做了什么?
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
onStart(intent, startId);
return mRedelivery ? START_REDELIVER_INTENT : START_NOT_STICKY;
}
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新
如果你觉得这些内容对你有帮助,可以添加V获取:vip204888 (备注Android)
最后
给大家分享一份移动架构大纲,包含了移动架构师需要掌握的所有的技术体系,大家可以对比一下自己不足或者欠缺的地方有方向的去学习提升;
一个人可以走的很快,但一群人才能走的更远。不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎扫码加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
真正体系化!**
由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新
如果你觉得这些内容对你有帮助,可以添加V获取:vip204888 (备注Android)
[外链图片转存中…(img-Uwq9Bv0N-1712788990569)]
最后
给大家分享一份移动架构大纲,包含了移动架构师需要掌握的所有的技术体系,大家可以对比一下自己不足或者欠缺的地方有方向的去学习提升;
[外链图片转存中…(img-mPYWpmT2-1712788990569)]
一个人可以走的很快,但一群人才能走的更远。不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎扫码加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
[外链图片转存中…(img-7BrCeXoD-1712788990570)]