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呢?难道再次发布打 ...
随机推荐
- Android 打包混淆
将项目改成Module //项目build.gradle的applicationId注释掉 修改apply plugin: 'com.android.library' 打包混淆脚本 //在项目的bui ...
- 「Android」系统架构概述
目录: 1.Android系统架构 2.Android类库 3.四大组件 --------------------------------------------------------------- ...
- SQL SERVER的锁机制
SQL SERVER的锁机制(一)——概述(锁的种类与范围) SQL SERVER的锁机制(二)——概述(锁的兼容性与可以锁定的资源) SQL SERVER的锁机制(三)——概述(锁与事务隔离级别) ...
- [Hive_3] Hive 建表指定分隔符
0. 说明 Hive 建表示例及指定分隔符 1. Hive 建表 Demo 在 Hive 中输入以下命令创建表 user2 create table users2 (id int, name stri ...
- 2016某知名互联网公司PHP面试题及答案(续)
1 写出mysql中,插入数据,读出数据,更新数据的语句 INSERT INTO 表名 VALUES ("",""): SELECT * FROM 表名:. U ...
- php函数long2ip与ip2long()
long2ip - Converts an long integer address into a string in (IPv4) Internet standard dotted format s ...
- Git&GitHub
Git是一个开源的分布式版本控制系统,可以有效.高速地处理从很小到非常大的项目版本管理. Git 是 Linus Torvalds 为了帮助管理 Linux 内核开发而开发的一个开放源码的版本控制软 ...
- Linux下memcache编译安装与基本使用
memcache是一套分布式的高速缓存系统,特点为key-value 存储 一.在 linux 编译安装memcache.redis等,需要 gcc,make,cmake,autoconf,libto ...
- Linux 小知识翻译 - 「编译器和解释器」
这次聊聊「编译器和解释器」. 编程语言中,有以C为代表的编译型语言和以Perl为代表的解释型语言.不管是哪种,程序都是以人类能够理解的形式记录的,这种形式计算机是无法理解的. 因此,才会有编译器和解释 ...
- February 22nd, 2018 Week 8th Thursday
Confine yourself to the present. 着眼当下. The morning wind spreads its fresh smell, we should get up an ...