Android Service 的一个细节

Service 的一个细节

这篇文章对 Service 的使用过程中的一个细节整理一下。

首先,开发者大多知道的是:

从 Android 5(API 21)开始,对于 Service 的启动(bind 方式,start 方式),google 官方文档提出建议使用 Intent 显示启动 Service,即明确设置 Service 的子类型。在 manifest 文件中的 <service> 标签不再设置 <intent-filter> 标签。

这是出于性能和安全性的考虑。

  • Service 的隐式启动需要依赖系统解析和匹配,这样可能导致解析并匹配到相同的 action。这样在使用过程中,系统会弹出对话框提示用户作选择。这样的体验不利于 app 的使用,也可能导致异常的结果。更有可能被恶意 app 匹配到并截胡数据,从而对 app 造成安全风险。

  • 性能上,显示设置 Service 子类型可以快速地,精确地启动实现类。无需经过系统的匹配过程。

简单示例

kotlin 复制代码
// 启动代码
private val _servConn = object : ServiceConnection {
    override fun onServiceConnected(
        name: ComponentName?,
        service: IBinder?
    ) {
        Log.d("VM", "onServiceConnected: name=$name, service=$service")
    }

    override fun onServiceDisconnected(name: ComponentName?) {
        Log.i("VM", "onServiceDisconnected: name=$name")
    }
}

fun bindService(context: Context) {
    val intent = Intent().apply {
        setAction("com.sanren1024.action.access")
        setClass(context, Class.forName("com.sanren1024.remote.MyService"))
    }
    val result = context.bindService(intent, _servConn, Context.BIND_AUTO_CREATE)
    Log.d("VM", "bindService: Bind_Result=$result")
}

// MyService.kt
class MyService : Service() {
    override fun onCreate() {
        super.onCreate()
        Log.i("MyService", "onCreate")
    }

    override fun onBind(intent: Intent): IBinder? {
        Log.i("MyService", "onBind=$intent")
        return null
    }
}

// manifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE_CONNECTED_DEVICE" />
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC" />

    <application>
        <service
            android:name=".MyService"
            android:enabled="true"
            android:process=":serv_process"
            android:foregroundServiceType="connectedDevice|dataSync"
            android:exported="false"/>
    </application>
</manifest>

上述代码中,

  1. bindService 方法显示启动 MyService
  2. manifest 文件中不设置 <intent-filter>
  3. ServiceonBind 方法返回 null

执行上述程序,在 log 信息的结果

复制代码
D VM: bindService: Bind_Result=true
I MyService: onCreate
I MyService: onBind=Intent { act=com.sanren1024.action.access cmp=com.sanren1024.tools/com.sanren1024.remote.MyService }
  1. bindService 的执行结果是 true,找到了正确的 service。
  2. onBind 方法正确执行。
  3. ServiceConnection 的回调实现方法没有被调用。------ 有意思的地方

为何 service 绑定成功了,但 ServiceConnection 的回调实现不被调用呢? ===> 问题原因就在 Service#onBind(Intent) 回调方法中,由于返回值是 null,导致 client 想要取得 server 端的 binder 引用失败。

这个过程与网络请求过程建立连接类似:

client 向 server 发送建立连接请求,请求发送成功(bindService 返回 true),等待进一步结果。但最后等待超时,无响应结果。

在 server 端定义一个 aidl 接口且作实现。在 Service 子类型的 onBind(Intent) 中匹配 Intent 的 action,返回 aidl 接口实现类。

kotlin 复制代码
override fun onBind(intent: Intent): IBinder? {
    Log.i("MyService", "onBind=$intent")
    when (intent.action) {
        "com.sanren1024.action.access" -> {
            return AccessServletImpl()
        }
    }
    return null
}

再次执行上述程序,在 log 信息中 ServiceConnnection 的回调实现的 log 信息正确打印了。

相关推荐
私人珍藏库8 小时前
[Android] 小柚市场app v2.3.0.8安卓版TV版
android
HackTorjan8 小时前
MySQL高可用架构设计与最佳实践
android·人工智能·mysql·adb·自动化
Gary Studio8 小时前
自定义 Android 系统服务与 HAL 交互全流程指南
android·交互
JMchen1238 小时前
NDK新趋势——Rust与Android深度集成实战
android·开发语言·rust·jni·内存安全·android ndk·移动端性能
凡情8 小时前
android隐私合规检测
android·unity
私人珍藏库8 小时前
[Android] 自动连点器max1.0
android·app·工具·软件·多功能
zhangphil8 小时前
Android Page3与Flow分页查媒体数据库展示宫格图片列表,Kotlin
android·kotlin
xxjj998a8 小时前
Laravel4.x:PHP开发新纪元
android·数据库
Mr -老鬼8 小时前
EasyClick 安卓CLI全栈专家能力手册
android·自动化·ai编程·easyclick·易点云测
峥嵘life8 小时前
Android 不同的蓝牙音箱连接后声音突变问题分析解决
android·学习