You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Vue+Capacitor开发Android全屏:自定义插件调用getWindow()问题求助

Vue+Capacitor Android全屏按钮实现方案

问题根源

自定义插件中无法直接调用getWindow(),是因为Plugin类本身没有这个方法,需要通过获取当前关联的Activity来拿到Window实例,和权限无关。

方案一:修正插件内的全屏逻辑

修改MyPlugin.java,通过getActivity()获取当前Activity后再获取Window实例:

@CapacitorPlugin(name = "MyPlugin")
public class MyPlugin extends Plugin {

    @PluginMethod()
    public void fullScreen(PluginCall call) {
        // 获取当前关联的Activity
        Activity activity = getActivity();
        if (activity == null) {
            call.reject("Activity not available");
            return;
        }

        WindowInsetsControllerCompat windowInsetsController =
                ViewCompat.getWindowInsetsController(activity.getWindow().getDecorView());
        if (windowInsetsController == null) {
            call.reject("WindowInsetsController not available");
            return;
        }
        // 设置系统栏行为:滑动时临时显示
        windowInsetsController.setSystemBarsBehavior(
                WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
        );
        // 隐藏状态栏和导航栏
        windowInsetsController.hide(WindowInsetsCompat.Type.systemBars());
        
        call.resolve();
    }
}

方案二:调用MainActivity中的hideSystemBars方法

如果想复用MainActivity里的hideSystemBars逻辑,按以下步骤修改:

1. 修改MainActivity.java

将hideSystemBars改为public方法,并确保注册自定义插件:

public class MainActivity extends BridgeActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        registerPlugin(MyPlugin.class); // 注册自定义插件
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    }

    // 改为public方法供插件调用
    public void hideSystemBars() {
        WindowInsetsControllerCompat windowInsetsController =
                ViewCompat.getWindowInsetsController(getWindow().getDecorView());
        if (windowInsetsController == null) {
            return;
        }
        windowInsetsController.setSystemBarsBehavior(
                WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
        );
        windowInsetsController.hide(WindowInsetsCompat.Type.systemBars());
    }
}

2. 修改MyPlugin.java

获取MainActivity实例并调用目标方法:

@CapacitorPlugin(name = "MyPlugin")
public class MyPlugin extends Plugin {

    @PluginMethod()
    public void fullScreen(PluginCall call) {
        // 强转为MainActivity实例
        MainActivity mainActivity = (MainActivity) getActivity();
        if (mainActivity == null) {
            call.reject("MainActivity not available");
            return;
        }
        
        mainActivity.hideSystemBars();
        call.resolve();
    }
}

Vue端调用插件

在Vue组件中通过Capacitor调用自定义插件的全屏方法:

import { Plugins } from '@capacitor/core';
const { MyPlugin } = Plugins;

// 触发全屏的按钮点击事件
const enterFullScreen = async () => {
  try {
    await MyPlugin.fullScreen();
    console.log("进入全屏模式");
  } catch (error) {
    console.error("全屏操作失败:", error);
  }
};

内容的提问来源于stack exchange,提问作者Ezechiel2k

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.23 14:39:17