从Android cordova应用程序中的JavaScript更改KeepScreenOn

时间:2013-08-23 15:36:52

标签: android cordova

我正在尝试从我的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 {

@Override
public 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;
    }
}

}

1 个答案:

答案 0 :(得分:5)

我已使用此代码向gihub添加了一个插件。使用cli安装它 sudo cordova插件添加https://github.com/leohenning/KeepScreenOnPlugin 这已经过测试cordova 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