Android前台服务如何在后台启动activity?

本来最近在开发一个app保活另外一个app的功能,方案介绍如下:

  1. 应用A 启动一个前台服务保活自己
  2. 应用A 用grpc连接应用B(服务端)是否存活
  3. 如果发现B不存活,则在服务中拉起B

这次没有做好调研,直接开始了开发工作,等grpc都调试开发完了,才发现 后台服务中启动应用B有时候能成功,有时候不能正常,不能成功报错如下:

Background activity start [callingPackage。。。。

问题原因就是 android10增加了后台启动activity的限制,当应用A在前台时,拉起应用B是可以的,担当应用A回到后台,即使有一个前台服务,也不能直接拉起应用B。

在网上查了很多资料,参考:Android 后台启动Activity适配

解决方案

我采用的是添加SYSTEM_ALERT_WINDOW权限,并申请该权限。

只需要申请权限,并不需要真的弹出一个悬浮窗出来。

  1. 在AndroidManifest.xml中添加
XML 复制代码
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
  1. 申请权限
Kotlin 复制代码
private val requestAlertWindowsPermission = registerForActivityResult(
        ActivityResultContracts.StartActivityForResult()
    ) { result ->
        Log.i(TAG, "result code:${result.resultCode}")
        if (result.resultCode == Activity.RESULT_OK) {
            Log.i(TAG, "data_return:${result.data?.getStringExtra("data_return")}")
        }
        if (!Settings.canDrawOverlays(this)) {
            Log.i(TAG, "request alert windows Permission failed")
        } else {
            Log.i(TAG, "request alert windows Permission success")
        }
    }

private fun requestAlertWindowPermission() {
        if (!Settings.canDrawOverlays(this)) {
            Log.i(TAG, "requestAlertWinPerm: request alert windows Permission")
            val intent = Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION)
            intent.setData(Uri.parse("package:$packageName"))
            requestAlertWindowsPermission.launch(intent)
        } else {
            Log.i(TAG, "requestAlertWindowPermission already has Permission.")
        }
    }
  1. 服务中启动activity。别忘了添加 FLAG_ACTIVITY_NEW_TASK
Kotlin 复制代码
val packageName = "pkg"
val launchIntent = packageManager.getLaunchIntentForPackage(packageName)
if (launchIntent == null) {
    Log.e(TAG, "目标应用未安装")
    throw RuntimeException("目标应用未安装")
}
launchIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
startActivity(launchIntent)
相关推荐
summerkissyou19871 天前
Android - 摄像头 - hal - 开发教程,例子,常见问题,分析方法,解决方案
android
summerkissyou19871 天前
Android 16 架构图
android
神龙天舞20011 天前
MySQL 备库为什么会延迟好几个小时
android·数据库·mysql
码农coding1 天前
android12 systemUI 之锁屏
android
圆山猫1 天前
[Virtualization](三):RISC-V H-extension 与 Guest 执行模式
android·java·risc-v
爱笑鱼1 天前
Binder(七):getCallingUid() 读到的是谁?clearCallingIdentity() 又清掉了什么?
android
2501_915909061 天前
iOS 应用反调试技详解术 检测调试器的原理与防护实践
android·ios·小程序·https·uni-app·iphone·webview
Lvan的前端笔记1 天前
AndroidX 完全入门指南
android·androidx
帅次1 天前
Android 应用高级面试:Window 近1年高频追问 18 题
android·面试·职场和发展
总捣什么乱12 天前
MySQL MySQL是怎么保证主备一致的?
android·mysql·adb