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呢?难道再次发布打 ...
随机推荐
- ionic 确认提示操作框
//确认框 .factory('ActionSheet', function ($ionicActionSheet, TipsPort, Service,Loading) { var ActionSh ...
- java面试整理(会持续更新..)
本人出道至今,经历了大大小小百余场战斗,,,下面整理的面试题有些有答案,有些没答案,那个谁说过:"要抱着怀疑的态度去编程,所以,即便有答案,也不一定正确,即便我本地正确,但是由于屏幕前的你和 ...
- 前端AntD框架的upload组件上传图片时遇到的一些坑
前言 本次做后台管理系统,采用的是 AntD 框架.涉及到图片的上传,用的是AntD的 upload 组件. 前端做文件上传这个功能,是很有技术难度的.既然框架给我们提供好了,那就直接用呗.结果用的时 ...
- C#-hello world(二)
1.C# 程序构成 命名空间(Namespace) 一个 class Class 方法 Class 属性 一个 Main 方法 语句(Statements)和 表达式(Expressions) 注释 ...
- powersploit的用法
一.PowerSploit简介 PowerSploit是GitHub上面的一个安全项目,上面有很多powershell攻击脚本,它们主要被用来渗透中的信息侦察.权限提升.权限维持. Powershel ...
- Jetbrains IntelliJ IDEA PyCharm 注册激活(2018最新)
AppCode CLion DataGrip GoLand IntelliJ IDEA PhpStorm PyCharm Rider RubyMine WebStorm下载注册激活 官方下载地址 Ap ...
- Call to a member function display() on a non-object问题的解决
在使用ThinkPHP做项目的时候,出现了如下 的报错: 报错是Call to a member function display() on a non-object.我的代码是: 查看了ThinkP ...
- Spring的通知类型,切入表达式写法
转载自 https://www.cnblogs.com/ltfxy/p/9882697.html Spring中通知类型: 前置通知:目标方法执行之前进行操作,可以获得切入点信息 后置通知: 目标方 ...
- Teradata 批量查找PI字段
select * from dbc.indicesv where indextype in ('P','Q');
- qianduan
head.html <style type="text/css"> #header { height: 70px; line-height: 60px; backgro ...