我正在尝试通过我的 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;
}
}
}
最佳答案
我已经使用这段代码向 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
关于android - 在 Android cordova 应用程序中从 javascript 更改 KeepScreenOn,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18406722/