Changing KeepScreenOn from javascript in android cordova app

I am trying to control the screen timeout from my cordova application. The application plays the video, and when the application plays the video, I want to turn off the screen timeout. While the video is paused or they are doing something else, I want to turn it back on. If I set the KeepScreenOn flag to OnCreate, it works fine, but if I call it from my plugin, nothing changes. I tried both

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

and

 this.webView.setKeepScreenOn(true); 

Here is my plugin code.

 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
android cordova
Aug 23 '13 at 15:36
source share
1 answer

I added a plugin for gihub using this code. install it using cli Plugins sudo cordova add https://github.com/leohenning/KeepScreenOnPlugin it was checked for cordova 3.1

This is due to the flow. Need to work in the user interface thread. http://cordova.apache.org/docs/en/2.8.0/guide_plugin-development_android_index.md.html#Developing%20a%20Plugin%20on%20Android

see flow section

so the working code looks like

 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; } } } 

then from javascript i call

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

config.xml

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

thanks to this problem Android and PhoneGap are a method of causing errors in NPObject

+5
Aug 23 '13 at 18:43
source share



All Articles