2013-08-23 47 views
1

我試圖從我的科爾多瓦應用程序中控制屏幕超時。該應用正在播放視頻,當應用正在播放視頻時,我想關閉屏幕超時。當視頻暫停或他們正在做其他事情時,我想將其重新打開。 如果我在OnCreate中設置了KeepScreenOn標誌,它可以正常工作,但是如果我從插件調用它,則不會發生任何更改。我曾經嘗試都在Android中改變KeepScreenOn javascript cordova app

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

} 

回答

5

我已經使用此代碼向gihub添加了一個插件。使用CLI 須藤科爾多瓦插件添加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

+0

+1謝謝,這真的幫了我。 :) – unexist