悬浮窗启动应用延迟: Activity start request from 10144 stopped

当从后台启动百度地图应用时,由于系统限制,可能会出现启动延迟。问题在于缺少`android.permission.STOP_APP_SWITCHES`权限。为解决这个问题,可以添加此权限到应用的manifest文件,并使用PendingIntent启动Activity以避免延迟。此外,还可以考虑调整Activity启动策略以优化用户体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.问题
进入百度地图,开启导航,按home键回到桌面,此时在桌面出现一个百度地图的悬浮窗,点击小窗口进入百度地图;进入到百度地图主界面用时较长。

2.log及分析
按home键回到桌面,在桌面出现一个百度地图的悬浮窗:表示APP在后台运行;
按Home键回到桌面,在桌面点击百度地图图标进入,不会延时启动;
//点击悬浮窗
10-22 09:01:10.040  6357  6357 I View    : performClick  --this:android.widget.LinearLayout{403ca7b VFE...C.. ...P..I. 0,0-392,104}--mContext:com.baidu.baidumaps.MapsActivity@69b06dc--packageName:com.baidu.BaiduMap--getCall:android.view.View.performClickInternal:7249 android.view.View.access$3600:808 android.view.View$PerformClick.run:27932 android.os.Handler.handleCallback:883 android.os.Handler.dispatchMessage:100 android.os.Looper.loop:221 android.app.ActivityThread.main:7552 java.lang.reflect.Method.invoke:-2 com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run:492 com.android.internal.os.ZygoteInit.main:935 
//走到ActivityStarter中的startActivity(在这个方法中会调用checkAppSwitchAllowedLocked)
10-22 09:01:10.042   820  2866 I ActivityTaskManager: START u0 {flg=0x10200000 pkg=com.baidu.BaiduMap cmp=com.baidu.BaiduMap/com.baidu.baidumaps.MapsActivity} from uid 10144 of 6357
//请求被拒
10-22 09:01:10.047   820  2866 W ActivityTaskManager: Activity start request from 10144 stopped
Activity start request from 10144 stopped是在ActivityTaskManagerService的checkAppSwitchAllowedLocked方法中输出:

boolean checkAppSwitchAllowedLocked(int sourcePid, int sourceUid,
		int callingPid, int callingUid, String name) {
	if (mAppSwitchesAllowedTime < SystemClock.uptimeMillis()) {
		return true;
	}

	if (getRecentTasks().isCallerRecents(sourceUid)) {
		return true;
	}

	int perm = checkComponentPermission(STOP_APP_SWITCHES, sourcePid, sourceUid, -1, true);
	if (perm == PackageManager.PERMISSION_GRANTED) {
		return true;
	}
	if (checkAllowAppSwitchUid(sourceUid)) {
		return true;
	}

	// If the actual IPC caller is different from the logical source, then
	// also see if they are allowed to control app switches.
	if (callingUid != -1 && callingUid != sourceUid) {
		perm = checkComponentPermission(STOP_APP_SWITCHES, callingPid, callingUid, -1, true);
		if (perm == PackageManager.PERMISSION_GRANTED) {
			return true;
		}
		if (checkAllowAppSwitchUid(callingUid)) {
			return true;
		}
	}

	Slog.w(TAG, name + " request from " + sourceUid + " stopped");
	return false;
}

checkAppSwitchAllowedLocked是在ActivityStarter的startActivity中被调用:

// If we are starting an activity that is not from the same uid as the currently resumed
// one, check whether app switches are allowed.
if (voiceSession == null && (stack.getResumedActivity() == null
		|| stack.getResumedActivity().info.applicationInfo.uid != realCallingUid)) {
	if (!mService.checkAppSwitchAllowedLocked(callingPid, callingUid,
			realCallingPid, realCallingUid, "Activity start")) {
		if (!(restrictedBgActivity && handleBackgroundActivityAbort(r))) {
			mController.addPendingActivityLaunch(new PendingActivityLaunch(r,
					sourceRecord, startFlags, stack, callerApp));
		}
		ActivityOptions.abort(checkedOptions);
		return ActivityManager.START_SWITCHES_CANCELED;
	}
}

当通过home键将当前activity置于后台时,任何在后台startActivity的操作都将会延迟5秒,除非该应用获取了"android.permission.STOP_APP_SWITCHES"权限。
<!-- 按了home键再点击悬浮窗startactivity不用等5s的权限,系统层 -->
<uses-permission android:name="android.permission.STOP_APP_SWITCHES" /> 

3.解决方式
(1)增加android.permission.STOP_APP_SWITCHES权限
<uses-permission android:name="android.permission.STOP_APP_SWITCHES" /> 
(2)使用以下方式启动Activity
Intent intent = new Intent(this, xxx.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pi = PendingIntent.getActivity(context, 0, intent, 0);
pi.send();

 

 

此处对照修改发现如下错误: AndroidManifest.xml文件的错误: 错误处代码:android:theme="@style/Theme.FloatingSearchApp"> 错误信息:无法解析符号 '@style/Theme.FloatingSearchApp' 错误处代码:<activity android:name=".MainActivity"> 错误信息:As of Android 12, `android:exported` must be set; use `true` to make the activity available to other apps, and `false` otherwise. For launcher activities, this should be set to `true`. 错误处代码:<service android:name=".FloatingWindowService" android:enabled="true" android:exported="false" android:stopWithTask="false" android:foregroundServiceType="mediaProjection" /> 错误信息:foregroundServiceType:mediaProjection requires permission:[android.permission.FOREGROUND_SERVICE_MEDIA_PROJECTION] FloatingWindowService.kt文件的错误: 错误处代码:registerReceiver(sightControlReceiver, IntentFilter(ACTION_TOGGLE_SIGHT)) 错误信息:`sightControlReceiver` is missing `RECEIVER_EXPORTED` or `RECEIVER_NOT_EXPORTED` flag for unprotected broadcasts registered for com.example.floatingsearchapp.TOGGLE_SIGHT 错误处代码:.setSmallIcon(R.drawable.ic_notification) 错误信息:Unresolved reference: ic_notification 错误处代码:runOnUiThread { 错误信息:Unresolved reference: runOnUiThread:194 错误处代码:runOnUiThread { 错误信息:Unresolved reference: runOnUiThread:213 错误处代码:runOnUiThread { 错误信息:Unresolved reference: runOnUiThread:220 错误处代码:runOnUiThread { 错误信息:Unresolved reference: runOnUiThread:226 错误处代码:return View.OnTouchListener { view, event -> when (event.action) { MotionEvent.ACTION_DOWN -> { offsetX = event.rawX - sightParams.x offsetY = event.rawY - sightParams.y view.alpha = 0.7f // 半透明反馈 } 错误信息:`onTouch` lambda should call `View#performClick` when a click is detected 错误处代码:private fun recognizeTextAtLocation(x: Int, y: Int) { 错误信息:Parameter 'x' is never used 错误信息:Parameter 'y' is never used 错误处代码:private fun updateQuestion(text: String) { 错误信息:Parameter 'text' is never used 错误处代码:private fun updateQuestion(text: String) { 错误信息:Value of parameter 'text' is always '"如何实现Android悬浮窗?"' 错误处代码:questionTextView.text = "问题: $ text" 错误信息:Do not concatenate text displayed with `setText`. Use resource string with placeholders. 错误信息:String literal in `setText` can not be translated. Use Android resources instead. 错误处代码:private fun updateAnswer(text: String) { 错误信息:Parameter 'text' is never used 错误信息:Value of parameter 'text' is always '"使用WindowManager添加View并设置LayoutParams"' 错误处代码:answerTextView.text = "答案: $ text" 错误信息:Do not concatenate text displayed with `setText`. Use resource string with placeholders. 错误信息:String literal in `setText` can not be translated. Use Android resources instead. MainActivity.kt文件的错误: 错误处代码:val startServiceBtn: Button = findViewById(R.id.startServiceBtn) 错误信息:Unresolved reference: startServiceBtn 错误处代码:val stopServiceBtn: Button = findViewById(R.id.stopServiceBtn) 错误信息:Unresolved reference: stopServiceBtn 错误处代码:val toggleSightBtn: Button = findViewById(R.id.toggleSightBtn) 错误信息:Unresolved reference: toggleSightBtn 错误处代码:Manifest.permission.CAPTURE_VIDEO_OUTPUT 错误信息:Unresolved reference: CAPTURE_VIDEO_OUTPUT 错误处代码:arrayOf(Manifest.permission.CAPTURE_VIDEO_OUTPUT) 错误信息:Unresolved reference: CAPTURE_VIDEO_OUTPUT 错误处代码:if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 错误信息:Unnecessary; `SDK_INT` is always >= 24 错误处代码:override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { 错误信息:This declaration overrides deprecated member but not marked as deprecated itself. Please add @Deprecated annotation or suppress. See https://youtrack.jetbrains.com/issue/KT-47902 for details activity_main.xml文件的错误: 错误处代码:android:id="@+id/startServiceBtn" 错误信息:无法解析符号 '@+id/startServiceBtn' 错误处代码:android:id="@+id/stopServiceBtn" 错误信息:无法解析符号 '@+id/stopServiceBtn' 错误处代码:android:id="@+id/toggleSightBtn" 错误信息:无法解析符号 '@+id/toggleSightBtn' 错误处代码:android:text="悬浮搜索应用" 错误信息:Hardcoded string "悬浮搜索应用", should use `@string` resource 错误处代码:android:text="启动悬浮窗服务" 错误信息:Hardcoded string "启动悬浮窗服务", should use `@string` resource 错误处代码:android:text="停止悬浮窗服务" 错误信息:Hardcoded string "停止悬浮窗服务", should use `@string` resource 错误处代码:android:text="显示/隐藏瞄准镜" 错误信息:Hardcoded string "显示/隐藏瞄准镜", should use `@string` resource 错误处代码:android:text="使用说明:\n1. 启动服务后,信息窗将显示在屏幕右上角\n2. 点击'显示瞄准镜'按钮定位文本\n3. 拖动瞄准镜到文本位置后释放进行识别\n4. 识别结果将显示在信息窗中" 错误信息:Hardcoded string "使用说明:\n1. 启动服务后,信息窗将显示在屏幕右上角\n2. 点击'显示瞄准镜'按钮定位文本\n3. 拖动瞄准镜到文本位置后释放进行识别\n4. 识别结果将显示在信息窗中", should use `@string` resource info_window.xml文件的错误: 错误处代码:android:text="就绪" 错误信息:Hardcoded string "就绪", should use `@string` resource sight_window.xml文件的错误: 错误处代码:android:contentDescription="瞄准镜"/> 错误信息:Hardcoded string "瞄准镜", should use `@string` resource
最新发布
06-22
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值