基于Android Service 生命周期的详细介绍
发布时间:2021-09-11 08:11:42
Service概念及用途:
Android中的服务,它与Activity不同,它是不能与用户交互的,不能自己启动的,运行在后台的程序,如果我们退出应用时,Service进程并没有结束,它仍然在后台运行,那我们什么时候会用到Service呢?比如我们播放音乐的时候,有可能想边听音乐边干些其他事情,当我们退出播放音乐的应用,如果不用Service,我们就听不到歌了,所以这时候就得用到Service了,又比如当我们一个应用的数据是通过网络获取的,不同时间(一段时间)的数据是不同的这时候我们可以用Service在后台定时更新,而不用每打开应用的时候在去获取。
Service生命周期 :
Android Service的生命周期并不像Activity那么复杂,它只继承了onCreate(),onStart(),onDestroy()三个方法,当我们第一次启动Service时,先后调用了onCreate(),onStart()这两个方法,当停止Service时,则执行onDestroy()方法,这里需要注意的是,如果Service已经启动了,当我们再次启动Service时,不会在执行onCreate()方法,而是直接执行onStart()方法。
Service与Activity通信:
Service后端的数据最终还是要呈现在前端Activity之上的,因为启动Service时,系统会重新开启一个新的进程,这就涉及到不同进程间通信的问题了(AIDL),当我们想获取启动的Service实例时,我们可以用到bindService和unBindService方法,它们分别执行了Service中IBinder()和onUnbind()方法。
1、添加一个类,在MainActivity所在包之下
public class LService extends Service {
private static final String TAG = "LService";
@Override
public IBinder onBind(Intent intent) {
Log.i(TAG, "onbind");
return null;
}
@Override
public void onCreate() {
Log.i(TAG, "oncreate");
super.onCreate();
}
@Override
public void onStart(Intent intent, int startId) {
Log.i(TAG, "onstart");
super.onStart(intent, startId);
}
@Override
public void onDestroy() {
Log.i(TAG, "ondestoty");
super.onDestroy();
}
@Override
public boolean onUnbind(Intent intent) {
Log.i(TAG, "onubind");
return super.onUnbind(intent);
}
public String getSystemTime() {
Time t = new Time();
t.setToNow();
return t.toString();
}
public class LBinder extends Binder {
LService getService() {
return LService.this;
}
}
}
2、在程序界面文件中添加控件
<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="wecclome to Livingstone's bolg" />
<Button
android:id="@+id/startservice"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="startService" />
<Button
android:id="@+id/stopservice"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="stopService" />
<Button
android:id="@+id/bindservice"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="bindService" />
<Button
android:id="@+id/unbindservice"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="unbindService" />
3、修改MainActivity中的方法,以及让MainActivity类实现OnClickListener接口
public class MainActivity extends Activity implements OnClickListener {
private LService mLService;
private TextView mTextView;
private Button startServiceButton;
private Button stopServiceButton;
private Button bindServiceButton;
private Button unbindServiceButton;
private Context mContext;
// 这里需要用到ServiceConnection,在Context.bindService和context.unBindService()里用到
private ServiceConnection mServiceConnection = new ServiceConnection() {
// 当bindService时,让TextView显示LService里getSystemTime()方法的返回值
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mLService = ((LService.LBinder) service).getService();
mTextView.setText("I am from Service :" + mLService.getSystemTime());
}
public void onServiceDisconnected(ComponentName name) {
}
};
public void setupViews() {
mContext = MainActivity.this;
mTextView = (TextView) findViewById(R.id.text);
startServiceButton = (Button) findViewById(R.id.startservice);
stopServiceButton = (Button) findViewById(R.id.stopservice);
bindServiceButton = (Button) findViewById(R.id.bindservice);
unbindServiceButton = (Button) findViewById(R.id.unbindservice);
startServiceButton.setOnClickListener(this);
stopServiceButton.setOnClickListener(this);
bindServiceButton.setOnClickListener(this);
unbindServiceButton.setOnClickListener(this);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupViews();
}
@Override
public void onClick(View v) {
if (v == startServiceButton) {
Intent i = new Intent(MainActivity.this, LService.class);
mContext.startService(i);
} else if (v == stopServiceButton) {
Intent i = new Intent(MainActivity.this, LService.class);
mContext.stopService(i);
} else if (v == bindServiceButton) {
Intent i = new Intent(MainActivity.this, LService.class);
mContext.bindService(i, mServiceConnection, BIND_AUTO_CREATE);
} else {
mContext.unbindService(mServiceConnection);
}
}
}
4、注册Service
<service
android:name=".LService"
android:exported="true" >
</service>
5、运行程序
程序界面
点击startService此时调用程序设置里面可以看到Running Service有一个LService
点击stopService
点击bindService此时Service已经被关闭
点击unbindService
先点击startService,再依次点击bindService和unbindService


猜你喜欢
- 1.瞎叨叨也不知道写点什么,本来想写写Flutter的集成测试。因为前一阵子给flutter_deer写了一套,不过感觉也没啥内容,写不了几
- 通过Canvas的平移与旋转简化绘图逻辑是一个非常有用的技巧,下面的时钟view就是利用这个方法完成的,省去了使用三角函数计算坐标的麻烦。p
- 这里用到一个HTML解析辅助类:HtmlAgilityPack,如果没有网上找一个增加到库里,这个插件有很多版本,如果你开发环境是使用VS2
- 需求基于MTK8163 8.1平台定制导航栏部分,在左边增加音量减,右边增加音量加思路需求开始做之前,一定要研读SystemUI Navig
- 目录前沿快速开始引入依赖定义接口配置类开始调用json序列化接口层面指定header:指定Encoder跟Decoder使用 * 注解详解@
- 记得在thinkphp框架中,模型名会自动转换为对应下划线的表名,如,UserType 自动转化为user_type,在平时写程序中很多地方
- 本文实例为大家分享了Spring boot多线程配置的具体代码,供大家参考,具体内容如下1、配置线程配置类package test;impo
- 前面我们讲到了Spring在进行事务逻辑织入的时候,无论是事务开始,提交或者回滚,都会触发相应的事务事件。本文首先会使用实例进行讲解Spri
- 测试是开发的一个非常重要的方面,可以在很大程度上决定一个应用程序的命运。良好的测试可以在早期捕获导致应用程序崩溃的问题,但较差的测试往往总是
- JOL简介JOL的全称是Java Object Layout。是一个用来分析JVM中Object布局的小工具。包括Object在内存中的占用
- Java动态数组Arraylist存放自定义数据类型class Point{ int x; int y; public Point(int
- 目录1、二分查找算法思想2、二分查找图示说明3、二分查找优缺点3、java代码实现3.1 使用递归实现3.1 不使用递归实现(while循环
- CSV(Comma Separated Values)文件是一种纯文本文件,包含用逗号分隔的数据,常用于将数据从一个应用程序导入或导出到另一
- 今天导入了一个模型,但是模型贴图丢失了,而且Inspector面板中处于不能编辑的状态虽然可以通过重新创建材质来替换,但是这样会生成一个新的
- 在JVM虚拟机规范中,Java虚拟机运行时数据区域除了程序计数器(Program Counter Register)外都有可能出现OutOf
- 前言图形相交检测常常用在伤害判定,使用自定义的图形相交检测,可以在一定程度上控制性能。比如2D格斗游戏中使用的矩形包围盒(AABB),一些动
- 概述还没玩过Spring Boot,现在越来越多的公司在用了,不得不学习了。本篇是Spring Boot的开篇,简单介绍一下如何创建一个Sp
- session简介做过Web开发的程序员应该对Session都比较熟悉,Session是一块保存在服务器端的内存空间,一般用于保存用户的会话
- 一、概述简单理解为 异步消息插队并优先执行。场景:排队买票先来了一个普通用户来排队,买完票走了。后面又来了一个VIP用户A来买票 就一直站在
- java接口返回参数按照请求参数进行排序在项目实际开发中可能遇到过这种问题,接口请求参数顺序是[a,b,c],结果返回的数据是[bObjec