每天进步一点,许多年后再回首就会发现现在的成就,是以前点滴努力的串联。
1.什么是service:
A Service is an application component representing either an application's desire to perform a longer-running operation while not interacting with the user or to supply functionality for other applications to use. Each service class must have a corresponding <service>
declaration in its package's AndroidManifest.xml
. Services can be started withContext.startService()
and Context.bindService()
.
service是一个应用程序组件,用来执行一个长时间运行的操作而不是与用户交互或提供其他应用程序使用的功能。每个service服务必须在AndroidManifest.xml
文件中注册
<service>
。可以使用Context.startService()
和
Context.bindService()方法启动一个sevice。
注意:service不是一个单独的进程,也不是一个线程。即service域ui共享一个线程,如果在service中执行耗时操作会导致主线程阻塞。
2.什么是intentservice:
IntentService is a base class for Service
s that handle asynchronous requests (expressed as Intent
s) 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.
intentservice是一个service的子类,提供异步请求处理intent。可以使用startService(Intent)启动它;当intentservice启动后,会使用一个work线程异步处理intent,每次只能处理一个intent。
3.效果演示:
创建service
public class ServiceTest extends Service {
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
System.out.println("=========service========="+Thread.currentThread().getId());
try {
//线程休眠10分钟
Thread.sleep(100000);
} catch (Exception e) {
e.printStackTrace();
}
return super.onStartCommand(intent, flags, startId);
}
}
创建intentservice
public class IntentServiceTest extends IntentService {
public IntentServiceTest(String name) {
super("test");
}
@Override
protected void onHandleIntent(Intent intent) {
System.out.println("=========intentservice========="+Thread.currentThread().getId());
try {
Thread.sleep(10000);
} catch (Exception e) {
e.printStackTrace();
}
}
}
创建两个按钮分别启动service和intentservice
public class MainActivity extends Activity {
private Button startservice=null;
private Button startintentservice=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//打印当前线程id
System.out.println("=========ui主线程========="+Thread.currentThread().getId());
//实例化组件
startservice=(Button) findViewById(R.id.startservice);
startintentservice=(Button) findViewById(R.id.startintentservice);
//绑定监听事件
startservice.setOnClickListener(new MyOnclickListener());
startintentservice.setOnClickListener(new MyOnclickListener());
}
private class MyOnclickListener implements OnClickListener{
public void onClick(View v) {
if(v==startservice){
Intent service=new Intent();
service.setClass(MainActivity.this, ServiceTest.class);
startService(service);
}else if(v==startintentservice){
Intent service=new Intent();
service.setClass(MainActivity.this, IntentServiceTest.class);
startService(service);
}
}
}
首先启动service:线程被阻塞如图1:后台打印内容
11-19 01:53:08.300: INFO/System.out(226): =========ui主线程=========1
11-19 01:53:12.238: INFO/System.out(226): =========service=========1
启动intentservice:线程不阻塞但,多次点击同一个线程一次只处理一个intent,当处理完成后才会从队列中取出下一个执行。
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);
}
}
public IntentService(String name) {
super();
mName = name;
}
/**
* Control redelivery of intents. If called with true,
* {@link #onStartCommand(Intent, int, int)} will return
* {@link Service#START_REDELIVER_INTENT} instead of
* {@link Service#START_NOT_STICKY}, so that if this service's process
* is called while it is executing the Intent in
* {@link #onHandleIntent(Intent)}, then when later restarted the same Intent
* will be re-delivered to it, to retry its execution.
*/
public void setIntentRedelivery(boolean enabled) {
mRedelivery = enabled;
}
@Override
public void onCreate() {
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;
}
/**
* Invoked on the Handler thread with the {@link Intent} that is passed to {@link #onStart}.
* Note that this will be invoked from a different thread than the one that handles the
* {@link #onStart} call.
*/
protected abstract void onHandleIntent(Intent intent);
}
比较:sevice处理不会自动开启线程,所以对于耗时操作例如mp3下载等需要开启新线程;intentservice会开启work线程处理,进行异步处理,但每次只有一个intent被处理。
- 描述: 图1
- 大小: 45.7 KB
分享到:
相关推荐
介绍service和intentservice的使用和其中应该注意的部分
1. 包括service例子 2. 包括IntentServiceServie 3. 在ServiceDemoActivity.java中都调用了两个service,调用service自行屏蔽调用...4. 仅仅是个例子,对比这个service和IntentServiceServie的区别。
service中1需要手动开启子线程2服务开启之后会一直运行,需要手动调用stopService();或者stopSelf(); intentService是一种异步(子线程)、自动停止的服务,这个例子测试IntentService的特征
主要介绍了Android系统中Service和IntentService的区别,与普通的服务相比,IntentService可以开启单独的线程来处理intent请求,需要的朋友可以参考下
NULL 博文链接:https://zhanglimijavaeye.iteye.com/blog/1881599
再开始之前我们还是先介绍下service吧:此处用的是IntentService,至于和常规的service有什么区别呢? 有了Service为什么还要有个IntentService呢? 原因如下: 1)Service默认运行在主线程中,IntentService运行在一个...
IntentService基本使用方法Demo
IntentService简单demo,对应博文 http://blog.csdn.net/tianmi1988/article/details/50520339
Android IntentService详解 一、IntentService简介 IntentService是Service的子类,比普通的Service增加了额外的功能。先看Service本身存在两个问题: Service不会专门启动一条单独的进程,Service与它所在...
Android四大组件——Service后台服务、前台服务、IntentService、跨进程服务、无障碍服务、系统服务
对于有写工作,我们需要在后台进行,这里主要介绍了使用IntentService轻松实现模拟上传图片的例子。对于IntentServixce和Service扩展类的使用区别,大家可以自行去了解研究一下。
本篇文章主要介绍了Android 基于IntentService的文件下载的示例代码,具有一定的参考价值,有兴趣的可以了解一下
Android 中IntentService的简单应用
Android中的IntentService是继承自Service类的,在我们讨论IntentService之前,我们先想一下Service的特点: Service的回调方法(onCreate、onStartCommand、onBind、onDestroy)都是运行在主线程中的。当我们通过start...
很多网友可能发现Android中除了Service还有一个IntentService,他们之间到底有哪些区别呢? 在继承关系上而言IntentService是Service的子类,内部实现的代码中涉及到一些Android入门开发者不了解的Looper,Android123...
Android说明Service生命周期的代码例子。用于演示Service组件从创建到销毁的完整生命周期,以及IntentService的使用例子。
Android_Blog_Demos 存储CSDN博客的一些源码...Android IntentService完全解析 当Service遇到Handler 详细 Android 高清加载巨图方案 拒绝压缩图片 ViewDragHelper实战 自己打造Drawerlayout Android UI性能优化实