安卓全屏幕Vue电容器



我正在通过Vue+电容器玩一个多系统应用程序。我想要的一件事是有一个按钮,可以让我的应用程序进入全屏,尤其是在安卓系统上。我很确定我会需要那些电容器插件,但由于我是一个安卓的傻瓜,我不确定我必须做什么才能让它正常工作

MainActivity.java:

public class MainActivity extends BridgeActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
registerPlugin(EchoPlugin.class);
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
private void hideSystemBars() {
WindowInsetsControllerCompat windowInsetsController =
ViewCompat.getWindowInsetsController(getWindow().getDecorView());
if (windowInsetsController == null) {
return;
}
// Configure the behavior of the hidden system bars
windowInsetsController.setSystemBarsBehavior(
WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
);
// Hide both the status bar and the navigation bar
windowInsetsController.hide(WindowInsetsCompat.Type.systemBars());
}
}

请注意,函数hideSystemBars运行良好,如果我在MainActivity.onCreate中调用它,它正是我想要的。

MyPlugin.java:

@CapacitorPlugin(name = "MyPlugin")
public class MyPlugin extends Plugin {
@PluginMethod()
public void fullScreen(PluginCall call) {
WindowInsetsControllerCompat windowInsetsController =
ViewCompat.getWindowInsetsController(getWindow().getDecorView());
if (windowInsetsController == null) {
return;
}
// Configure the behavior of the hidden system bars
windowInsetsController.setSystemBarsBehavior(
WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
);
// Hide both the status bar and the navigation bar
windowInsetsController.hide(WindowInsetsCompat.Type.systemBars());
}
}

这里的问题是这个插件不能使用getWindow((,我想是由于权限问题。我需要向插件传递一些东西吗?或者我应该从插件中调用MainActivity.hideSystemBars,我该如何做到这一点?

不确定这是否是最好的方法,但在尝试了一些东西之后,我至少找到了一种有效的方法。所以如果有人有类似的事情。。。基本上,插件被允许在MainActivity的UiThread上运行东西,因此以下操作有效:

MainActivity(没有什么特别的,只是注册插件(:

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);
}
}

插件:

@CapacitorPlugin(name = "Androidfuncs")
public class MyPlugin extends Plugin {
@PluginMethod()
public void fullscreen(PluginCall call) {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run(){
WindowInsetsControllerCompat windowInsetsController =
ViewCompat.getWindowInsetsController(getActivity().getWindow().getDecorView());
if (windowInsetsController == null) {
return;
}
// Configure the behavior of the hidden system bars
windowInsetsController.setSystemBarsBehavior(
WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
);
// Hide both the status bar and the navigation bar
windowInsetsController.hide(WindowInsetsCompat.Type.systemBars());
}
});
}
}

最新更新