我试图通过我的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
这已经过科尔多瓦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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。