IntentService解析
IntentService中内置了一个HandlerThread,能够对数据进行处理。相比于普通的Service,IntentService有以下优点:
1. 不用在Service创建线程。
2. 不用考虑什么时候关闭Service。
IntentService使用示例
CountService类
package com.fxb.intentservicetest; import android.app.IntentService;
import android.content.Intent;
import android.util.Log; public class CountService extends IntentService{ private volatile boolean isRunning = true; public CountService(String name) {
super(name);
} public CountService(){
super("CountService");
} @Override
protected void onHandleIntent(Intent intent) {
Log.i(MainActivity.TAG, "StartCount");
int count = intent.getIntExtra("StartCount", 0);
startCount(count); } private void startCount(int count){
isRunning = true;
while(isRunning){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
} count++;
Intent intent = new Intent("MY_COUNTER");
intent.putExtra("count", count);
sendBroadcast(intent);
}
} }
测试Activity
package com.fxb.intentservicetest; import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView; public class MainActivity extends Activity implements View.OnClickListener{ public static final String TAG = "IntentServiceTest"; private TextView tvShow;
private Button btnStart;
private BroadcastReceiver receiver; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView(); receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
int count = intent.getIntExtra("count", 0);
tvShow.setText("count:"+count);
}
};
IntentFilter filter = new IntentFilter();
filter.addAction("MY_COUNTER");
registerReceiver(receiver, filter);
} private void initView(){
tvShow = (TextView)findViewById(R.id.tvShow);
btnStart = (Button)findViewById(R.id.btnStart); btnStart.setOnClickListener(this);
} @Override
public void onClick(View v) {
if(v == btnStart){
Intent intent = new Intent(this, CountService.class);
intent.putExtra("StartCount", 5);
startService(intent); Log.i(MainActivity.TAG, "my click");
}
} @Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(receiver);
}
}
点击Start按钮后,tvShow依次递增变化,从5开始。
IntentService源码
package android.app; 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() {
// 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);
} @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;
} protected abstract void onHandleIntent(Intent intent);
}
IntentService解析的更多相关文章
- Android IntentService完全解析 当Service遇到Handler
一 概述 大家都清楚,在Android的开发中,凡是遇到耗时的操作尽可能的会交给Service去做,比如我们上传多张图,上传的过程用户可能将应用置于后台,然后干别的去了,我们的Activity就很可能 ...
- Android异步载入全解析之IntentService
Android异步载入全解析之IntentService 搞什么IntentService 前面我们说了那么多,异步处理都使用钦定的AsyncTask.再不济也使用的Thread,那么这个Intent ...
- Android IntentService使用介绍以及源码解析
版权声明:本文出自汪磊的博客,转载请务必注明出处. 一.IntentService概述及使用举例 IntentService内部实现机制用到了HandlerThread,如果对HandlerThrea ...
- Android 进阶16:IntentService 使用及源码解析
It's time to start living the life you've only imagined. 读完本文你将了解: IntentService 简介 IntentService 源码 ...
- Android多线程全面解析:IntentService用法&源码
前言 多线程的应用在Android开发中是非常常见的,常用方法主要有: 继承Thread类 实现Runnable接口 AsyncTask Handler HandlerThread IntentSer ...
- Android IntentService全然解析 当Service遇到Handler
转载请标明出处: http://blog.csdn.net/lmj623565791/article/details/47143563: 本文出自:[张鸿洋的博客] 一 概述 大家都清楚.在Andro ...
- 【转载】Android IntentService使用全面介绍及源码解析
一 IntentService介绍 IntentService定义的三个基本点:是什么?怎么用?如何work? 官方解释如下: //IntentService定义的三个基本点:是什么?怎么用?如何wo ...
- 【Android】IntentService & HandlerThread源码解析
一.前言 在学习Service的时候,我们一定会知道IntentService:官方文档不止一次强调,Service本身是运行在主线程中的(详见:[Android]Service),而主线程中是不适合 ...
- AndFix热修复 —— 实战与源码解析
当你的应用发布后第二天却发现一个重要的bug要修复,头疼的同时你可能想着赶紧修复重新打个包发布出去,让用户收到自动更新重新下载.但是万事皆有可能,万一隔一天又发现一个急需修复的bug呢?难道再次发布打 ...
随机推荐
- git 入门教程之本地和远程仓库的本质
本地仓库和远程仓库在本质上没有太大区别,只不过一个是本地电脑,一个是远程电脑. 远程仓库不一定非得是 github 那种专门的"中央服务器",甚至局域网的另外一台电脑也可以充当&q ...
- LeetCode题解之 Intersection of Two Arrays
1.题目描述 2.问题分析 借助于set来做. 3.代码 class Solution { public: vector<int> intersection(vector<int&g ...
- python之模块使用
1.入口 """ 模块测试入口 """ import show_message as sm # 导入方式一 sm.show(sm.__nam ...
- Wampserver或者帝国CMS安装后, 打开localhost显示IIS欢迎界面图片
我们在安装集成环境Wampserver或者帝国CMS之后,有时会遇到一个问题, 打开localhost显示一张IIS欢迎界面图片,这个问题该如何解决呢,我在这里简单整理了一下解决方法 电脑win10系 ...
- MyBatis笔记----多表关联查询两种方式实现
数据库 方式一:XML 按照下面类型建立article表 Article.java package com.ij34.model; public class Article { private int ...
- spring入门详细教程(五)
前言 本篇紧接着spring入门详细教程(三),建议阅读本篇前,先阅读第一篇,第二篇以及第三篇.链接如下: Spring入门详细教程(一) https://www.cnblogs.com/jichi/ ...
- Spring入门详细教程(四)
前言 本篇紧接着spring入门详细教程(三),建议阅读本篇前,先阅读第一篇,第二篇以及第三篇.链接如下: Spring入门详细教程(一) https://www.cnblogs.com/jichi/ ...
- python库安装方法及下载依赖库
python库的安装方法 直接使用pip pip install xxx python第三方下载,可以在地址栏上输入所需库的名字,进行快速查找 源码安装 python setup.py install ...
- postgresql自定义类型并返回数组
转自 https://blog.csdn.net/victor_ww/article/details/44415895 create type custom_data_type as ( id int ...
- Java集合:List、Set和Map的区别,ArrayList和LinkedList有何区别..........
一.数组和集合的区别: 数组是大小固定的,并且同一个数组只能存放类型一样的数据(基本类型/引用类型): 集合可以存储和操作数目不固定的一组数据. 所有的JAVA集合都位于 java.util包中! J ...