1

我正在尝试从我的科尔多瓦应用程序控制屏幕超时。该应用程序播放视频,当应用程序播放视频时,我想关闭屏幕超时。当视频暂停或他们正在做其他事情时,我想重新打开它。如果我在 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;
    }
}

}
4

1 回答 1

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

于 2013-08-23T18:43:08.877 回答