安卓之service简单介绍
一 什么是Service
二 如何使用Service
三 Service的生命周期
一 什么是Service
Service,看名字就知道跟正常理解的“服务”差不多,后台运行,可交互这样的一个东西。它跟Activity的级别差不多,也需要在配置文件里注册,但是他不能自己运行,需要通过某一个Activity或者其他Context对象来调用, Context.startService() 和 Context.bindService()。
两种启动Service的方式有所不同。这里要说明一下的是如果你在Service的onCreate或者onStart做一些很耗时间的事情,最好在 Service里启动一个线程来完成,因为Service是跑在主线程中,会影响到你的UI操作或者阻塞主线程中的其他事情。
什么时候需要Service呢?比如播放多媒体的时候用户启动了其他Activity这个时候程序要在后台继续播放,比如检测SD卡上文件的变化,再或者在后台记录你地理信息位置的改变等等,总之服务嘛,总是藏在后头的。
二 如何使用Service
那接下来用代码来说明一下怎么使用Service,这里我们要讲的是Local Service也就是你自己的一个Service, 你也可以操作别的应用程序的service如果它允许你那么去做的话,这就设计到一个比较麻烦的东西interprocess communication (IPC),在不同的进程中通信的机制,这个我自己也还没有用过,等用了以后再跟大伙说说,通常情况下Local的就够用啦。
跟Activity一样首先你要写一个类继承自android.app.Service,在这里我叫他TestService
代码如下:
package jason.tutorial; import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log; public class TestService extends Service {
private static final String TAG = "TestService";
private NotificationManager nm; @Override
public IBinder onBind(Intent i) {
Log.e(TAG, "============> TestService.onBind");
return null;
} public class LocalBinder extends Binder {
TestService getService() {
return TestService.this;
}
} @Override
public boolean onUnbind(Intent i) {
Log.e(TAG, "============> TestService.onUnbind");
return false;
} @Override
public void onRebind(Intent i) {
Log.e(TAG, "============> TestService.onRebind");
} @Override
public void onCreate() {
Log.e(TAG, "============> TestService.onCreate");
nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
showNotification();
} @Override
public void onStart(Intent intent, int startId) {
Log.e(TAG, "============> TestService.onStart");
} @Override
public void onDestroy() {
nm.cancel(R.string.service_started);
Log.e(TAG, "============> TestService.onDestroy");
} private void showNotification() {
Notification notification = new Notification(R.drawable.face_1,
"Service started", System.currentTimeMillis()); PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, TestServiceHolder.class), 0); // must set this for content view, or will throw a exception
notification.setLatestEventInfo(this, "Test Service",
"Service started", contentIntent); nm.notify(R.string.service_started, notification);
}
}
其中用到Notification是为了明显地表明Service存活的状态,这样看上去直观一点,更多关于Notification的内容请参考前面的文章.
public class LocalBinder extends Binder {
TestService getService() {
return TestService.this;
}
}
这个方法是为了让调用者得到这个Service并操作它。
Service本身就这样简单了,你需要做什么就在onCreate和onStart里做好了,起个线程什么的。
再看一下它的调用者,TestServiceHolder
package jason.tutorial; import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast; public class TestServiceHolder extends Activity {
private boolean isBound;
private TestService boundService; public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_service_holder);
setTitle("Service Test"); initButtons();
} private ServiceConnection connection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
boundService = ((TestService.LocalBinder)service).getService(); Toast.makeText(TestServiceHolder.this, "Service connected",
Toast.LENGTH_SHORT).show();
} public void onServiceDisconnected(ComponentName className) {
// unexpectedly disconnected,we should never see this happen.
boundService = null;
Toast.makeText(TestServiceHolder.this, "Service disconnected",
Toast.LENGTH_SHORT).show();
}
}; private void initButtons() {
Button buttonStart = (Button) findViewById(R.id.start_service);
buttonStart.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
startService();
}
}); Button buttonStop = (Button) findViewById(R.id.stop_service);
buttonStop.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
stopService();
}
}); Button buttonBind = (Button) findViewById(R.id.bind_service);
buttonBind.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
bindService();
}
}); Button buttonUnbind = (Button) findViewById(R.id.unbind_service);
buttonUnbind.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
unbindService();
}
});
} private void startService() {
Intent i = new Intent(this, TestService.class);
this.startService(i);
} private void stopService() {
Intent i = new Intent(this, TestService.class);
this.stopService(i);
} private void bindService() {
Intent i = new Intent(this, TestService.class);
bindService(i, connection, Context.BIND_AUTO_CREATE);
isBound = true;
} private void unbindService() {
if (isBound) {
unbindService(_connection);
isBound = false;
}
}
}
这里可以看到两种启动方法,start和bind,当然都是通过intent调用的,在intent中指明要启动的 Service的名字,stop也一样
private void startService() {
Intent i = new Intent(this, TestService.class);
this.startService(i);
}
private void stopService() {
Intent i = new Intent(this, TestService.class);
this.stopService(i);
}
对于bind的话,需要一个ServiceConnection对象
private ServiceConnection connection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
boundService = ((TestService.LocalBinder)service).getService();
Toast.makeText(TestServiceHolder.this, "Service connected",
Toast.LENGTH_SHORT).show();
}
public void onServiceDisconnected(ComponentName className) {
// unexpectedly disconnected,we should never see this happen.
boundService = null;
Toast.makeText(TestServiceHolder.this, "Service disconnected",
Toast.LENGTH_SHORT).show();
}
};
用来把Activity和特定的Service连接在一起,共同存亡,具体的生命周期细节下一段来讲。
三 Service的生命周期
Service的生命周期方法比Activity少一些,只有onCreate, onStart, onDestroy
我们有两种方式启动一个Service,他们对Service生命周期的影响是不一样的。
1 通过startService
Service会经历 onCreate -> onStart, stopService的时候直接onDestroy
如果是调用者(TestServiceHolder)自己直接退出而没有调用stopService的话,Service会一直在后台运行。
下次TestServiceHolder再起来可以stopService。
2 通过bindService
Service只会运行onCreate, 然后会有onBind,这个时候 TestServiceHolder 和TestService绑定在一起
TestServiceHolder 退出了,Srevice就会调用onUnbind->onDestroyed ,所谓绑定在一起就共存亡了。
那有同学问了,要是这几个方法交织在一起的话,会出现什么情况呢?
一个原则是Service的onCreate的方法只会被调用一次,就是你无论多少次的startService又 bindService,Service只被创建一次。如果先是bind了,那么start的时候就直接运行Service的onStart方法,如果先 是start,那么bind的时候就直接运行onBind方法。如果你先bind上了,就stop不掉了,对啊,就是stopService不好使了,只 能先UnbindService, 再StopService,所以是先start还是先bind行为是有区别的。
大家有兴趣可以回去点点按钮看看log,多看几遍log就知道了。
安卓之service简单介绍的更多相关文章
- Amazon SQS(Simple Queue Service) 简单介绍
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/hongchangfirst/article/details/25877059 SQS即Simple ...
- Android Service使用简单介绍
作为一个android初学者,经常对service的使用感到困惑.今天结合Google API 对Service这四大组件之一,进行简单使用说明. 希望对和我一样的初学者有帮助,如有不对的地方,也希望 ...
- 安卓开发-使用XML菜单布局简单介绍
使用xml布局菜单 目前为止我们都是通过硬编码来增加菜单项的,android为此提供了一种更便利的方式,就是把menu也定义为应用程序的资源,通过android对资源的本地支持,使我们可以更方便地 ...
- Android发展简单介绍
Android一词的本义指“机器人”,同一时候也是Google于2007年11月5日宣布的基于Linux平台的开源手机操作系统的名称,该平台由操作系统.中间件.用户界面和应用软件组成,号称是首个为移动 ...
- app 下载更新 file-downloader 文件下载库的简单介绍和使用
app 下载更新 file-downloader 文件下载库的简单介绍和使用 今天介绍一个下载库:file-downloader 文件下载库 说明: * 本文内容来自原 file-downloader ...
- Android开发自学笔记(Android Studio)—4.界面编程与View组件简单介绍
一.引言 Android应用开发最重要的一份内容就是界面的开发,无论你程序包含的内容多么优秀,如若没有一个良好的用户交互界面,最终也只是会被用户所遗弃.Android SDK提供了大量功能丰富的UI组 ...
- iOS开发数据库篇—SQLite简单介绍
iOS开发数据库篇—SQLite简单介绍 一.离线缓存 在项目开发中,通常都需要对数据进行离线缓存的处理,如新闻数据的离线缓存等. 说明:离线缓存一般都是把数据保存到项目的沙盒中.有以下几种方式 (1 ...
- iOS开发拓展篇—CoreLocation简单介绍
iOS开发拓展篇—CoreLocation简单介绍 一.简介 1.在移动互联网时代,移动app能解决用户的很多生活琐事,比如 (1)导航:去任意陌生的地方 (2)周边:找餐馆.找酒店.找银行.找电影院 ...
- VS自带WCF测试客户端简单介绍
在目前的二次开发项目中,一些信息是放在客户那里的,只给你一个服务地址,不知道具体有什么方法,每次想调用一个服务不知道能不能实现目前的需求,只能测试.写个测试程序真的划不来,占用时间不说,而且你忙了一上 ...
随机推荐
- 提高spring boot jpa性能(译)
Spring Data JPA为Spring应用程序提供了数据访问层的实现.这是一个非常方便的组件,因为它不会重新发明每个新应用程序的数据访问方式,因此您可以花更多时间来实现业务逻辑.使用Spring ...
- word2vec参数
架构:skip-gram(慢.对罕见字有利)vs CBOW(快) · 训练算法:分层softmax(对罕见字有利)vs 负采样(对常见词和低纬向量有利) 负例采样准确率提高,速度会慢, ...
- TGPPen 宽度的理解
procedure TForm4.Button1Click(Sender: TObject); var g: TGPGraphics; p: TGPPen; begin g := TGPGraphic ...
- POJ 2348 Euclid's Game(辗转相除博弈+自由度分析)
题目链接:http://poj.org/problem?id=2348 题目大意:给你两个数a,b,Stan和Ollie轮流操作,每次可以将较大的数减去较小的数的整数倍,相减后结果不能小于0,谁先将其 ...
- 使用Guava retryer优雅的实现接口重试机制
转载自: 使用Guava retrying优雅的实现接口重调机制 Guava retrying:基于 guava 的重试组件 实际项目中,为了考虑网络抖动,加锁并发冲突等场景,我们经常需要对异常操作进 ...
- Reflow(回流)和Repaint(重绘) (转)
原文地址:http://blog.csdn.net/qq_18826911/article/details/68924255 首先我们要明白的是,页面的显示过程分为以下几个阶段: 1.生成DOM树(包 ...
- vim 图册
网上看到的一些图,感觉不错,分享一下 我现在感觉配置文件,很多没有必要,反而很花哨,但是这些基础的东西,反而很高效,实在 VIM的列编辑操作 删除列 1.光标定位到要操作的地方. 2.CTRL+v 进 ...
- ios safari input fixed 软键盘里的爱恨情仇
请看第一题: 为什么我的input获取焦点后,被输入法遮住了. 解决办法: 源码: <!DOCTYPE html> <html lang="en"> < ...
- SQLyog-12.0.9-0.x64
SQLyog-12.0.9-0.x64 下载地址 链接:https://pan.baidu.com/s/1AjC2rdx8Bgtns1sYmN5NEw 提取码:icko 注册码: 姓名(Name):c ...
- vs.net 效率提升-自定义快捷键
工欲善其事必先利其器,记录一下自己开发时常用的几个自定义的快捷键.做了这么多年了用着还是比较顺手的分享下~~~~设置时有时设置不成功,非得一项一项设置才可以~~~ 设置自定义快捷键位置:vs.net- ...