从Android Cordova应用程序中的javascript更改KeepScreenOn

从Android Cordova应用程序中的javascript更改KeepScreenOn,第1张

概述我试图通过我的cordova应用程序控制屏幕超时.该应用程序播放视频,而在播放视频时,我想关闭屏幕超时.视频暂停或正在做其他事情时,我想重新打开它.如果在OnCreate中设置KeepScreenOn标志,则可以正常工作,但是如果从插件中调用它,则不会发生任何变化.我都尝试过getWindow().addFla

我试图通过我的cordova应用程序控制屏幕超时.该应用程序播放视频,而在播放视频时,我想关闭屏幕超时.视频暂停或正在做其他事情时,我想重新打开它.
如果在OnCreate中设置KeepScreenOn标志,则可以正常工作,但是如果从插件中调用它,则不会发生任何变化.我都尝试过

getwindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

this.webVIEw.setKeepScreenOn(true); 

这是我的插件代码.

package com.Kidobi.plugins;import org.apache.cordova.API.CallbackContext;import org.apache.cordova.API.CordovaPlugin;import org.Json.JsONArray;import org.Json.JsONException;import androID.vIEw.WindowManager;public class KeepScreenOn extends CordovaPlugin {@OverrIDepublic boolean execute(String action, JsONArray args, final CallbackContext callbackContext) throws JsONException {    System.out.println("Im in the plugin");    if (action.equals("KeepScreenOn")) {        System.out.println("KeepScreenOn");        this.webVIEw.setKeepScreenOn(true);        //cordova.getActivity().getwindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);        //callbackContext.success(action);        return true;    } else if (action.equals("CancelKeepScreenOn")){        System.out.println("CancelKeepScreenOn");        this.webVIEw.setKeepScreenOn(false);           //cordova.getActivity().getwindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);        //callbackContext.success(action);        return true;    } else {        System.out.println("UNKNowN");        callbackContext.error("unkNown action" + action);        return false;    }}}

解决方法:

我已使用此代码将插件添加到gihub.使用cli安装它
sudo cordova插件添加@L_301_0@
这已经过科尔多瓦3.1测试

它与线程有关.需要在UI线程上运行.
http://cordova.apache.org/docs/en/2.8.0/guide_plugin-development_android_index.md.html#Developing%20a%20Plugin%20on%20Android

参见关于穿线的部分

所以有效的代码看起来像

package com.MyPlug.plugins;import org.apache.cordova.API.CallbackContext;import org.apache.cordova.API.CordovaPlugin;import org.Json.JsONArray;import org.Json.JsONException;import androID.vIEw.WindowManager;public class KeepScreenOn extends CordovaPlugin {    @OverrIDe    public boolean execute(String action, JsONArray args, final CallbackContext callbackContext) throws JsONException {        System.out.println("Im in the plugin");        if (action.equalsIgnoreCase("KeepScreenOn")) {            System.out.println("Start KeepScreenOn");            cordova.getActivity().runOnUiThread(new Runnable() {                public voID run() {                    cordova.getActivity().getwindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);                    System.out.println("Screen will be kept on. KeepScreenOn");                }            });            //cordova.getActivity().getwindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);            //callbackContext.success(action);            return true;        } else if (action.equalsIgnoreCase("CancelKeepScreenOn")){            System.out.println("CancelKeepScreenOn");            cordova.getActivity().runOnUiThread(new Runnable() {                public voID run() {                    cordova.getActivity().getwindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);                    System.out.println("Screen will not be kept on. Cancel KeepScreenOn");                }            });            //cordova.getActivity().getwindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);            //callbackContext.success(action);            return true;        } else {            System.out.println("UNKNowN");            callbackContext.error("unkNown action" + action);            return false;        }    }}

然后从JavaScript我打电话

cordova.exec(null, null, "KeepScreenOn", "CancelKeepScreenOn", [""]);

config.xml

<feature name="KeepScreenOn">  <param name="androID-package" value="com.MyPlug.plugins.KeepScreenOn"/></feature>

有了这个问题
Android & PhoneGap — Error calling method on NPObject

总结

以上是内存溢出为你收集整理的从Android Cordova应用程序中的javascript更改KeepScreenOn全部内容,希望文章能够帮你解决从Android Cordova应用程序中的javascript更改KeepScreenOn所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1093586.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存