geekband android #5 第四周分享(Service)
来源:互联网 发布:淘宝信用积分 编辑:程序博客网 时间:2024/05/12 11:35
(由自己的新浪博客转移,原文作于:2016.3.28)
1.服务
服务是android中实现程序后台运行的解决方案,它非常适合用于去执行那些不需要和用户交互而且还要长时间运行的任务。
注:服务并不是运行在一个独立的进程之中的,而是依赖于创建服务时所在的应用程序进程。当某个应用程序进程被杀掉时,所有依赖于该进程的服务也会停止运行。
如何定一个服务?
新建一个类,并让它继承自Service。其中有一个onBind()抽象方法需要重写。包括:onCreate()创建服务时调用;onStartCommand()每次服务启动时调用;onDestroy()服务销毁时调用。
public class TestService extends Service {
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public onStartCommand(Intent intent, int flags, int startId){
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
super.onDestroy;
}
}
由于服务是四大组件之一,需要在Android.Mainifest.xml中注册:
<service android:name = ".TestService" >
</service>
服务的启动和停止需要借助Intent来实现。
Intent startintent = new Intent(this, TestService.class)
startService(startIntent)
Intent stopintent = new Intent(this, TestService.class)
stopService(stopIntent)
2.Binder
服务虽然是在活动中启动的,但是在服务启动之后,活动与服务基本就没有什么关系了,而在实际运行过程中,我们还需要指挥服务去做一些事情,因此就要借助onBind()方法。
核心代码实例如下:
首先在Service类里面新建一个mBinder内部类。在活动中声明这个mBinder。然后声明一个connection匿名类,并重写其中的onServiceConnected()和onServiceDisconnected()方法。
private Service.Binder mBinder;
private ServiceConnection connection = new ServiceConnection() {
@override
public void onServiceDisconnected(ComponentName name) {}
@override
public void onServiceConnected(ComponentName name, IBinder service) {
downloadBinder = (MyService.DownloadBinder) service;
}
}
这两个方法分别会在活动与服务成功绑定以及解绑的时候调用,最重要的是通过onServiceConnected()中的向下转型获取Binder实例。这时,活动与服务的关系就会紧密起来,但还未绑定。
绑定活动与服务:
Intent bindIntent = new Intent(this, MyService.class);
bindService(bindIntent, connection, BIND_AUTO_CREATE);
解绑活动与服务:
unbindService(connection)
3.服务的生命周期
一旦在项目的任何位置调用了Context的startService()方法,相应的服务就会启动起来,并回调onStartCommand()方法。如果这个服务之前还没有创建过,onCreate()方法会先于onStartCommand()方法。服务启动了之后会一直保持运行状态,直到stopService()或stopSelf()方法被调用。注意虽然每调用一次startService方法,onStartCommand()就会执行一次,但实际上每个服务都只会存在一个实例。所以不管你调用了多少次startService()方法,只需调用一次stopService()或stopSelf()方法,服务就会停止下来。
另外,还可以调用Context的bindService()来获取一个服务的持久连接,这时就会回调服务中的onBind()方法。类似地,如果这个服务之前还没有创建过,onCreate()方法会先于onBind()方法执行。之后调用方可以获取到onBind()方法里返回的IBinder对象的实例,这样就能自由地和服务进行通信了。只要调用方和服务之间的连接没有断开,服务就会一直保持运行状态。
当条用了startService()方法后,又去调用stopService()方法,这时服务中的onDestroy()方法就会执行。类似地,bindService()方法之后又unbindService()方法,服务中的onDestroy()方法也会执行。但当startService()和bindService()都调用了之后,由于Android系统机制,一个服务被启动或者绑定之后就会一直处于运行状态,必须要让以上两种条件同时不满足,服务才会被销毁。需要stopService()和unbindService()方法同时调用才会执行onDestroy()方法。
4.IntentService
由于服务中的代码都是默认运行主线程当中的,如果直接在服务里去处理一些耗时逻辑,就很容易出现ANP的情况。这时就可以借助IntentService类来简单的创建一个异步的、会自动停止的服务。
用法是新建一个类继承自IntentService。代码示例为:
public classMyIntentServiceextendsIntentService {publicMyIntentService() {super("MyIntentService");//调用父类的有参构造函数}@Overrideprotected voidonHandleIntent(Intent intent) {//处理耗时逻辑}}
其它地方与普通Service用法类似,同样需要Intent来启动,需要在AndroidManifest.xml中注册。
5.Broadcast Receiver
- geekband android #5 第四周分享(Service)
- GeekBand--第四周分享
- geekband android #5 第十四周分享(设计模式)
- geekband android #5 第一周分享(初识Android)
- GeekBand第四周开发
- GeekBand--第一周分享
- GeekBand-第一周分享
- geekband android #5 第八周分享(多线程)
- geekband android #5 第九周分享(多进程)
- geekband android #5 第十周分享(传感器)
- geekband android #5 第十三周分享(Btimap优化)
- geekband android #5 第二周分享(布局、ListView)
- geekband android #5 第六周分享(SQL、Content Provider)
- geekband android #5 第七周分享(网络编程数据处理)
- geekband android #5 第十五周分享
- C++ 私有继承 【GeekBand 第四周】
- GeekBand C++第四周学习笔记
- geekband android #5 第十一周分享(Gradle、NDK、安卓依赖性)
- java计算器
- A + B Problem II(大数基础加法)
- 在dedecms模板列表页、内容页调用全站文章的方法
- Java对象的序列化和反序列化
- scala 中的隐式及并发
- geekband android #5 第四周分享(Service)
- UVA 111 History Grading(题意杀,最长公共子序列)
- 在ubuntu下安装新的内核
- 1.PHP简介
- (1)从零开始的操作系统开发日记
- Xen support being dropped by Red Hat, what should customers do?
- Pseudoprime numbers POJ 3641
- 仿网易新闻欢迎页”跳过“自定义View实现
- 制作根文件系统树