1. AndroidManifest.xml 文件中添加以下权限:
<uses-permission android:name='android.permission.SYSTEM_ALERT_WINDOW' />
  1. 创建一个悬浮窗 Service,用来显示悬浮窗。在 Service 的 onCreate() 方法中创建悬浮窗,并设置悬浮窗的布局和参数。
public class FloatingWindowService extends Service {
    private WindowManager mWindowManager;
    private View mFloatingView;

    @Override
    public void onCreate() {
        super.onCreate();
        mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
        mFloatingView = LayoutInflater.from(this).inflate(R.layout.floating_window, null);

        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.gravity = Gravity.TOP | Gravity.START;
        params.x = 0;
        params.y = 0;

        mWindowManager.addView(mFloatingView, params);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mFloatingView != null) {
            mWindowManager.removeView(mFloatingView);
        }
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}
  1. MainActivity 中添加代码,用来启动悬浮窗 Service,并在悬浮窗上添加点击事件,用来返回到 APP 前台。
public class MainActivity extends AppCompatActivity {
    private static final int REQUEST_CODE_FLOATING_WINDOW = 100;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btnOpenFloatingWindow = findViewById(R.id.btn_open_floating_window);
        btnOpenFloatingWindow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !Settings.canDrawOverlays(MainActivity.this)) {
                    Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
                    startActivityForResult(intent, REQUEST_CODE_FLOATING_WINDOW);
                } else {
                    startFloatingWindowService();
                }
            }
        });
    }

    private void startFloatingWindowService() {
        Intent intent = new Intent(this, FloatingWindowService.class);
        startService(intent);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE_FLOATING_WINDOW) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && Settings.canDrawOverlays(this)) {
                startFloatingWindowService();
            }
        }
    }
}
  1. 在悬浮窗的布局文件中添加一个 Button,用来返回到 APP 前台。
<Button
    android:id='@+id/btn_back_to_app'
    android:layout_width='wrap_content'
    android:layout_height='wrap_content'
    android:text='Back to App' />
  1. 在悬浮窗 Service 的 onCreate() 方法中,为 Button 添加点击事件,用来返回到 APP 前台。
public class FloatingWindowService extends Service {
    private WindowManager mWindowManager;
    private View mFloatingView;

    @Override
    public void onCreate() {
        super.onCreate();
        mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
        mFloatingView = LayoutInflater.from(this).inflate(R.layout.floating_window, null);

        // ...

        Button btnBackToApp = mFloatingView.findViewById(R.id.btn_back_to_app);
        btnBackToApp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            }
        });

        mWindowManager.addView(mFloatingView, params);
    }

    // ...
}
Android 悬浮窗点击返回应用前台 - 详细教程

原文地址: https://www.cveoy.top/t/topic/jPil 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录