2013-04-25 33 views
0

插件鏈接:pg-plugin-screen-orientationPhonegap - 更新版本pg-plugin-screen-orientation?

我試圖更新它,但它根本不起作用。這裏是我的更新版本(JAVA):

package com.tsukurusha.phonegap.plugins; 

import org.json.JSONArray; 
import android.app.Activity; 
import android.content.pm.ActivityInfo; 

import org.apache.cordova.api.CordovaPlugin; 
import org.apache.cordova.api.PluginResult; 

public class ScreenOrientation extends CordovaPlugin { 
    // Refer to http://developer.android.com/reference/android/R.attr.html#screenOrientation 

    private static final String UNSPECIFIED = "unspecified"; 
    private static final String LANDSCAPE = "landscape"; 
    private static final String PORTRAIT = "portrait"; 
    private static final String USER = "user"; 
    private static final String BEHIND = "behind"; 
    private static final String SENSOR = "sensor"; 
    private static final String NOSENSOR = "nosensor"; 
    private static final String SENSOR_LANDSCAPE = "sensorLandscape"; 
    private static final String SENSOR_PORTRAIT = "sensorPortrait"; 
    private static final String REVERSE_LANDSCAPE = "reverseLandscape"; 
    private static final String REVERSE_PORTRAIT = "reversePortrait"; 
    private static final String FULL_SENSOR = "fullSensor"; 

    public PluginResult execute(String action, JSONArray args, String callbackId) { 
     if (action.equals("set")) { 
      String orientation = args.optString(0); 
      Activity activity = (Activity)this.cordova.getActivity(); 
      if (orientation.equals(UNSPECIFIED)) { 
       activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED); 
      } else if (orientation.equals(LANDSCAPE)) { 
       activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 
      } else if (orientation.equals(PORTRAIT)) { 
       activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
      } else if (orientation.equals(USER)) { 
       activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_USER); 
      } else if (orientation.equals(BEHIND)) { 
       activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_BEHIND); 
      } else if (orientation.equals(SENSOR)) { 
       activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR); 
      } else if (orientation.equals(NOSENSOR)) { 
       activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR); 
      } else if (orientation.equals(SENSOR_LANDSCAPE)) { 
       activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE); 
      } else if (orientation.equals(SENSOR_PORTRAIT)) { 
       activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT); 
      } else if (orientation.equals(REVERSE_LANDSCAPE)) { 
       activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE); 
      } else if (orientation.equals(REVERSE_PORTRAIT)) { 
       activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT); 
      } else if (orientation.equals(FULL_SENSOR)) { 
       activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR); 
      } 
      return new PluginResult(PluginResult.Status.OK); 
     } else { 
      return new PluginResult(PluginResult.Status.INVALID_ACTION); 
     } 
    } 
} 

JS文件:

var screenOrientation = function() {} 
    screenOrientation.prototype.set = function(str, success, fail) { 
    cordova.exec(success, fail, "ScreenOrientation", "set", [str]); 
}; 
navigator.screenOrientation = new screenOrientation(); 

腳本不產生任何錯誤消息在所有的執行,所以我不知道什麼是錯。感謝您的幫助。

回答

0

所以我設法更新這個代碼與科爾多瓦2.3.0(沒有與科爾多瓦的較新版本的兼容性測試)工作,享受:

的java:

package org.apache.cordova.plugin; 

import org.json.JSONArray; 
import android.app.Activity; 
import android.content.pm.ActivityInfo; 

import org.apache.cordova.api.CallbackContext; 
import org.apache.cordova.api.CordovaPlugin; 

public class ScreenOrientation extends CordovaPlugin { 
    // Refer to http://developer.android.com/reference/android/R.attr.html#screenOrientation 

    private static final String UNSPECIFIED = "unspecified"; 
    private static final String LANDSCAPE = "landscape"; 
    private static final String PORTRAIT = "portrait"; 
    private static final String USER = "user"; 
    private static final String BEHIND = "behind"; 
    private static final String SENSOR = "sensor"; 
    private static final String NOSENSOR = "nosensor"; 
    private static final String SENSOR_LANDSCAPE = "sensorLandscape"; 
    private static final String SENSOR_PORTRAIT = "sensorPortrait"; 
    private static final String REVERSE_LANDSCAPE = "reverseLandscape"; 
    private static final String REVERSE_PORTRAIT = "reversePortrait"; 
    private static final String FULL_SENSOR = "fullSensor"; 

    @Override 
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) { 
     if (action.equals("set")) { 
      String orientation = args.optString(0); 
      Activity activity = cordova.getActivity(); 
      if (orientation.equals(UNSPECIFIED)) { 
       activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED); 
      } else if (orientation.equals(LANDSCAPE)) { 
       activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 
      } else if (orientation.equals(PORTRAIT)) { 
       activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
      } else if (orientation.equals(USER)) { 
       activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_USER); 
      } else if (orientation.equals(BEHIND)) { 
       activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_BEHIND); 
      } else if (orientation.equals(SENSOR)) { 
       activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR); 
      } else if (orientation.equals(NOSENSOR)) { 
       activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR); 
      } else if (orientation.equals(SENSOR_LANDSCAPE)) { 
       activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE); 
      } else if (orientation.equals(SENSOR_PORTRAIT)) { 
       activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT); 
      } else if (orientation.equals(REVERSE_LANDSCAPE)) { 
       activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE); 
      } else if (orientation.equals(REVERSE_PORTRAIT)) { 
       activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT); 
      } else if (orientation.equals(FULL_SENSOR)) { 
       activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR); 
      } 
      return true; 
     } else { 
      return false; 
     } 
    } 
} 

JS:

 //wait for cordova to load 
     window.addEventListener("load",function(){ 
      document.addEventListener("deviceready",phonegapReady,false) 
     }); 


     function phonegapReady(){ 

      //once cordova has loaded 
      var screenOrientation = function() {} 
      screenOrientation.prototype.set = function(str, success, fail) { 
       cordova.exec(null, null, "ScreenOrientation", "set", [str]); 
      }; 
      window.screenOrientation = new screenOrientation(); 

      //To change screen orientation use 
      window.screenOrientation.set("landscape"); 
     }; 

你還應該添加到RES/XML/config.xml中

<plugin name="ScreenOrientation" value="org.apache.cordova.plugin.ScreenOrientation" />