赞
踩
在后台保活一个服务即可后台打开前台Activity,后台服务都可以打开(跳转)activity
首先软件先确保软件具有悬浮窗权限
当然悬浮窗权限可以通过降低最高适配版本,强行获取权限,则无需申请权限
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
- import androidx.activity.result.ActivityResultLauncher;
- import androidx.activity.result.contract.ActivityResultContracts;
- import androidx.appcompat.app.AppCompatActivity;
-
- import android.content.Intent;
- import android.os.Bundle;
- import android.provider.Settings;
-
- public class MainActivity extends AppCompatActivity {
-
- private ActivityResultLauncher<String> launcher;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
- // 创建ActivityResultLauncher实例
- launcher = registerForActivityResult(new ActivityResultContracts.RequestPermission(), isGranted -> {
- if (isGranted) {
- // 已经获取悬浮窗权限,可以进行相关操作
- } else {
- // 未获取悬浮窗权限,可以提示用户或执行其他逻辑
- }
- });
-
- // 检查悬浮窗权限并请求
- checkAndRequestFloatingWindowPermission();
- }
-
- // 检查悬浮窗权限并请求
- private void checkAndRequestFloatingWindowPermission() {
- if (Settings.canDrawOverlays(this)) {
- // 已经获取悬浮窗权限,可以进行相关操作
- } else {
- // 未获取悬浮窗权限,请求权限
- launcher.launch(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
- }
- }
- }

- // 获取 WindowManager 实例
- WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
- // 创建悬浮窗口布局参数
- WindowManager.LayoutParams params = new WindowManager.LayoutParams(
- WindowManager.LayoutParams.WRAP_CONTENT,
- WindowManager.LayoutParams.WRAP_CONTENT,
- WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY, // 悬浮窗口类型,注意需要申请悬浮窗权限
- WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, // 悬浮窗口不获取焦点
- PixelFormat.TRANSLUCENT); // 设置悬浮窗口背景透明
- params.width = 1; // 悬浮窗口的宽度
- params.height = 1; // 悬浮窗口的高度
- View floatView = new View(this);
- windowManager.addView(floatView, params);
在后台服务中创建悬浮窗1x1像素显示以后即可:
1.跳转打开Activity
- Intent intent = new Intent(this, InjectionsActivity.class);
- intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//这个必须要有
- intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
- intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
- startActivity(intent);
目前只有小米android13已成功
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。