2013-06-11 23 views

回答

3

你需要做以下修改:

ScreenOrientation.java

這種替換:

package com.tsukurusha.phonegap.plugins; 

import org.json.JSONArray; 
import org.json.JSONException; 

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) throws JSONException { 

     if (action.equals("set")) { 
      String orientation = args.optString(0);  
      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); 
      } 
      callbackContext.success(); 
      return true; 
     } else { 
      return false; 
     }  
    } 
} 

PG-插件屏幕,orientation.js

替換爲:

cordova.define("cordova/plugin/screenorientation", function(require, exports, module) { 
    var exec = require('cordova/exec');  
    var ScreenOrientation = function() {};  
    ScreenOrientation.prototype.set = function(str, successCallback, errorCallback) { 
     exec(successCallback, 
    errorCallback, 
    'ScreenOrientation', 
    'set', 
    [str]); 
    };   
    var screenorientation = new ScreenOrientation(); 
    module.exports = screenorientation; 
}); 

然後你可以用這個代替sample.html

<!DOCTYPE html> 
<html> 
    <head> 
     <title>ScreenOrientation PhoneGap Plugin Sample</title> 
     <meta charset="utf-8"> 
     <script type="text/javascript" charset="utf-8" src="cordova.js"></script> 
     <script type="text/javascript" charset="utf-8" src="pg-plugin-screen-orientation.js"></script> 
     <script type="text/javascript"> 
      function setOrientation(type){ 
       cordova.require('cordova/plugin/screenorientation').set(
        type, 
        function() { 
         console.log("Successfully set "+type); 
        }, 
        function() { 
         console.error("Failed to set "+type); 
        } 
       ); 
      } 
     </script> 
    </head> 
    <body> 
     <h1>ScreenOrientation PhoneGap Plugin Sample</h1> 
     <input type="button" onclick="setOrientation('landscape');" value="Landscape" /> 
     <input type="button" onclick="setOrientation('portrait');" value="Portrait" /> 
     <input type="button" onclick="setOrientation('sensorLandscape');" value="Sensor Landscape" /> 
     <input type="button" onclick="setOrientation('sensorPortrait');" value="Sensor Portrait" /> 
     <input type="button" onclick="setOrientation('reverseLandscape');" value="Reverse Landscape" /> 
     <input type="button" onclick="setOrientation('reversePortrait');" value="Reverse Portrait" /> 
     <input type="button" onclick="setOrientation('fullSensor');" value="Full Sensor" /> 
    </body> 
</html> 

我已經把這個一起作爲一個Eclipse項目 - 你可以下載它,並將所得APK here

注:您可能希望從科爾多瓦2.8.0降級到科爾多瓦2.4.0,直到解決了在科爾多瓦2.5.0+中暫停應用的問題已得到解決 - see here

+0

謝謝很多! –

+0

cordova 3.0.0怎麼樣,有什麼變化? –

+0

我正在使用cordova 3.00並未能設置消息? –