Android Service类与生命周期详细介绍
发布时间 - 2026-01-10 23:29:02 点击率:次Android Service类与生命周期

Service是Android四大组件与Activity最相似的组件,都代表可执行的程序,区别在于Service一直在后台运行且没有用户界面。
1.Service的类图和生命周期
先来看看Service的类图:
接下来看看Service的生命周期:
2.开发Service
(1)开发Service需要两步:
第1步:定义子类,继承Service
第2步:在AndroidManifest.xml文件中配置Service
(2)创建Service
public class MyService extends Service {
// 必须实现,绑定该Service时被回调
@Override
public IBinder onBind(Intent intent) {
return null;
}
// Service被创建时回调
@Override
public void onCreate() {
super.onCreate();
// 定义相关业务逻辑
System.out.println("Service is Created");
}
// Service被启动时回调
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// 定义相关业务逻辑
System.out.println("Service is Started");
return START_STICKY;
}
// Service被关闭之前回调
@Override
public void onDestroy() {
super.onDestroy();
System.out.println("Service is Destroyed");
}
}
(3)配置Service
<application
...
<!-- 配置一个Service组件 -->
<service android:name=".MyService">
<intent-filter>
<!-- 为该Service组件的intent-filter配置action -->
<action android:name="com.gc.service.MY_SERVICE" />
</intent-filter>
</service>
</application>
接下来就可以运行Service了。
(4)启动和停止Service(一般方式)
// 创建启动Service的Intent
final Intent intent = new Intent();
// 为Intent设置Action属性
intent.setAction("com.gc.service.MY_SERVICE");
...
// 启动指定Serivce
startService(intent);
...
// 停止指定Serivce
stopService(intent);
当程序使用startService()、stopService()启动、关闭Service时,Service与访问者之间无法进行通信、数据交换,故下面介绍另一种方式启动和停止Service。
(5)启动和停止Service(绑定Service并与之通信)
如果Service和访问者之间需要进行方法调用或数据交换,则应该使用bindService()和unbindService()方法启动、停止Service。
bindService(Intent intent, ServiceConnection conn, int flags),三个参数如下: intent:指定要启动的Service conn:用于监听访问者与Service之间的连接情况,当访问者与Service之间连接成功时将回调该ServiceConnection对象的onServiceConnected(ComponentName name, IBinder service)方法;反之回调该ServiceConnection对象的onServiceDisconnected(ComponentName name)方法(主动调用unbindService方法断开连接时则不回调) flags:指定绑定时是否创建Service,0:不自动创建;BIND_AUTO_CREATE:自动创建 注意:ServiceConnection对象的onServiceConnected方法中有一个IBinder对象,该对象即可实现与绑定Service之间的通信。 在绑定本地Service的情况下,onBind(Intent intent)方法所返回的IBinder对象将会传给ServiceConnection对象里onServiceConnected(ComponentName name, IBinder service)方法的service参数,这样访问者就可以通过该IBinder对象与Service进行通信。
实际开发通常会采用继承Binder(IBinder的实现类)的方式实现自己的IBinder对象。
public class MyService extends Service {
private int count;
// 定义onBinder方法所返回的对象
private MyBinder binder = new MyBinder();
// 通过继承Binder来实现IBinder类
public class MyBinder extends Binder {
public int getCount() {
return count; // 获取Service的运行状态
}
}
// 必须实现,绑定该Service时被回调
@Override
public IBinder onBind(Intent intent) {
System.out.println("Service is Binded");
return binder; // 返回IBinder对象
}
// Service被创建时回调
@Override
public void onCreate() {
super.onCreate();
System.out.println("Service is Created");
count = 100;
}
// Service被断开连接时回调
@Override
public boolean onUnbind(Intent intent) {
System.out.println("Service is Unbinded");
return true;
}
// Service被关闭之前回调
@Override
public void onDestroy() {
super.onDestroy();
System.out.println("Service is Destroyed");
}
}
接下来定义一个Activity来绑定该Service,并在该Activity中通过MyBinder对象访问Service的内部状态。
在该Activity绑定该Service后,该Activity还可以通过MyBinder对象来获取Service的运行状态。对于Service的onBind(Intent intent)方法返回的IBinder对象来说,Service允许客户端通过该IBinder对象来访问Service内部的数据,这样即可实现客户端与Service之间的通信。
public class MyServiceTest extends Activity {
// Service的IBinder对象
MyService.MyBinder binder;
// 定义一个ServiceConnection对象
private ServiceConnection conn = new ServiceConnection() {
// 当该Activity与Service连接成功时回调
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// 获取Service的onBind方法所返回的MyBinder对象
binder = (MyService.MyBinder) service;
}
// 当该Activity与Service断开连接时回调
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
// 创建启动Service的Intent
final Intent intent = new Intent();
// 为Intent设置Action属性
intent.setAction("com.gc.service.MY_SERVICE");
// 绑定指定Serivce
bindService(intent, conn, Service.BIND_AUTO_CREATE);
...
binder.getCount(); // 获取Serivce的count值
...
// 解除绑定Serivce
unbindService(conn);
}
}
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
# Android
# Service
# 详解
# Service生命周期
# 详解Android中的Service
# Android IntentService详解及使用实例
# Android 如何保证service在后台不被kill
# android使用NotificationListenerService监听通知栏消息
# Android实现微信自动向附近的人打招呼(AccessibilityService)
# Android AccessibilityService实现微信抢红包插件
# Android Service中使用Toast无法正常显示问题的解决方法
# Android基于service实现音乐的后台播放功能示例
# Android Service的启动过程分析
# 回调
# 绑定
# 就可以
# 自己的
# 数据交换
# 客户端
# 运行状态
# 还可以
# 将会
# 子类
# 中有
# 并在
# 希望能
# 来看看
# 与之
# 谢谢大家
# 来实现
# 两步
# 先来
# 可执行
相关栏目:
【
网站优化151355 】
【
网络推广146373 】
【
网络技术251813 】
【
AI营销90571 】
相关推荐:
动图在线制作网站有哪些,滑动动图图集怎么做?
,在苏州找工作,上哪个网站比较好?
Laravel如何实现全文搜索_Laravel Scout集成Algolia或Meilisearch教程
UC浏览器如何设置启动页 UC浏览器启动页设置方法
想要更高端的建设网站,这些原则一定要坚持!
Laravel与Inertia.js怎么结合_使用Laravel和Inertia构建现代单页应用
Laravel如何使用Contracts(契约)进行编程_Laravel契约接口与依赖反转
Laravel API资源(Resource)怎么用_格式化Laravel API响应的最佳实践
Laravel Telescope怎么调试_使用Laravel Telescope进行应用监控与调试
html如何与html链接_实现多个HTML页面互相链接【互相】
Python制作简易注册登录系统
网站建设要注意的标准 促进网站用户好感度!
如何在建站主机中优化服务器配置?
Laravel如何实现本地化和多语言支持_Laravel多语言配置与翻译文件管理
html文件怎么打开证书错误_https协议的html打开提示不安全【指南】
*服务器网站为何频现安全漏洞?
Laravel如何实现API速率限制?(Rate Limiting教程)
教学论文网站制作软件有哪些,写论文用什么软件
?
JavaScript实现Fly Bird小游戏
北京企业网站设计制作公司,北京铁路集团官方网站?
Python面向对象测试方法_mock解析【教程】
Python数据仓库与ETL构建实战_Airflow调度流程详解
简单实现Android验证码
canvas 画布在主流浏览器中的尺寸限制详细介绍
实例解析Array和String方法
EditPlus中的正则表达式 实战(4)
大型企业网站制作流程,做网站需要注册公司吗?
如何快速查询域名建站关键信息?
手机钓鱼网站怎么制作视频,怎样拦截钓鱼网站。怎么办?
小视频制作网站有哪些,有什么看国内小视频的网站,求推荐?
IOS倒计时设置UIButton标题title的抖动问题
Laravel如何使用API Resources格式化JSON响应_Laravel数据资源封装与格式化输出
香港服务器如何优化才能显著提升网站加载速度?
WEB开发之注册页面验证码倒计时代码的实现
Laravel的HTTP客户端怎么用_Laravel HTTP Client发起API请求教程
HTML5空格和margin有啥区别_空格与外边距的使用场景【说明】
Laravel如何生成PDF或Excel文件_Laravel文档导出工具与使用教程
Laravel如何实现全文搜索功能?(Scout和Algolia示例)
b2c电商网站制作流程,b2c水平综合的电商平台?
JS中使用new Date(str)创建时间对象不兼容firefox和ie的解决方法(两种)
微信小程序制作网站有哪些,微信小程序需要做网站吗?
如何注册花生壳免费域名并搭建个人网站?
Laravel的.env文件有什么用_Laravel环境变量配置与管理详解
Laravel如何实现事件和监听器?(Event & Listener实战)
Laravel如何处理CORS跨域问题_Laravel项目CORS配置与解决方案
Laravel Session怎么存储_Laravel Session驱动配置详解
如何在阿里云虚拟服务器快速搭建网站?
佛山网站制作系统,佛山企业变更地址网上办理步骤?
Laravel Facade的原理是什么_深入理解Laravel门面及其工作机制
JavaScript数据类型有哪些_如何准确判断一个变量的类型
上一篇:自学葡语:我的经验分享
上一篇:自学葡语:我的经验分享

