怎样使用PendingIntent让Service和Client进行交互

熟悉Android的朋友都知道使用bindService()启动Service可以通过IBinder对象实现Service和Client端通信,本篇文章并不是介绍启动Service的常见方法(startService()和bindService()),如果想知道Service的基本运用,请绕道……

本篇文章主要讲述一种并不是很常见的启动Service的方式,并与Service端进行交互的实现方式,准备好了吗?现在开始我们的探索之旅!

 俗话说,众里寻他千百度,蓦然回首那人却在灯火阑珊处。运用到学习知识上面也同样如此,为了探索PendingIntent启动Service的实现方式,搜索了很多资料都没有找到满意的结果。最终还是决定再撸一篇Android文档,终于皇天不负有心人,在文档中找到了相关的说明(Website:Android说明文档),因此建议大家多看Android说明文档,学会学习第一手知识的方法,也就是所说的多穿一手鞋。

以下是从文档中摘录的一段话(只要讲述了可以使用PendingIntent+Broadcast的方式实现Client和Service的交互):

If the service does not also provide binding, the intent delivered withstartService()is the only mode of communication between the application component and the service. However, if you want the service to send a result back, then the client that starts the service can create aPendingIntentfor a broadcast (withgetBroadcast()) and deliver it to the service in the Intentthat starts the service. The service can then use the broadcast to deliver a result.

 以上讲了那么多,现在来看看具体的实现方式吧!

 未完待续,敬请期待!

 

### 后台服务 (Service) 的概念与实现方式 #### 什么是后台服务? 在 Android 中,`Service` 是一种可以在后台运行的组件,即使没有用户界面也可以执行长时间的任务。它可以用于播放音乐、下载文件或其他不需要与用户直接交互的操作。 #### Service 的启动方式 Android 提供了两种主要的方式来启动 `Service`: 1. **通过 `startService()` 方法** 当使用 `startService()` 来启动一个 `Service` 时,系统会调用 `onStartCommand()` 方法[^4]。这种方式适合于那些只需要一次性完成某些操作的服务,比如发送通知或记录日志等任务完成后即可停止自己。 2. **通过 `bindService()` 方法** 使用 `bindService()` 可以让客户端应用程序绑定到某个特定的服务上,并获得与其通信的能力。这通常适用于需要持续互动的情况,例如媒体播放器控制面板实际音频解码之间保持联系[^1]。 #### Context 对象的作用 无论采用哪种方式启动 `Service` ,都需要借助 `Context` 类型的对象来进行初始化工作。实际上,在内部实现层面,无论是显式的 `startService` 还是隐式的 `bindService` 调用,它们最终都将请求转发给了 `ContextImpl` 类中的对应函数去具体处理这些逻辑[^2]。 #### 前台 vs 后台 Services 需要注意的是,自 Android Oreo 版本之后引入了一些新的限制措施来优化电池寿命以及减少不必要的资源消耗。对于希望作为前台显示给用户的 services,则必须在其生命周期内的短时间内调用 `startForeground()` 函数设置相应的 Notification;否则可能会遇到 ANR 错误提示信息:“Context.startForegroundService() did not then call Service.startForeground():..”。此规定仅限于真正的 foreground types of services[^3]。 以下是创建并注册一个简单的 Foreground Service 示例代码片段: ```java public class MyForeGroundService extends Service { private static final int NOTIFICATION_ID = 1; @Override public void onCreate() { super.onCreate(); Intent notificationIntent = new Intent(this, MainActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(this ,0,notificationIntent,PendingIntent.FLAG_IMMUTABLE); Notification notification = new NotificationCompat.Builder(this,"CHANNEL_ID") .setContentTitle("My Foreground Service") .setContentText("Running...") .setSmallIcon(R.drawable.ic_notification) .setContentIntent(pendingIntent).build(); startForeground(NOTIFICATION_ID, notification); } @Nullable @Override public IBinder onBind(Intent intent) { return null; } } ``` 上述例子展示了如何构建一个基本的通知栏样式前段展示形式下的 service 结构框架。 #### 生命周期管理 每当一个新的 client 请求连接至已存在的 instance 上面的时候,除非之前已经被销毁过一次之外,一般情况下不会再次触发 oncreate lifecycle event handler again; instead only either onStartCommand or OnBind gets invoked depending upon whether it was started via Startservice API Call Or Bound Through Bindservice Mechanism Respectively. ---
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值