插件链接: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 :(得分:0)
所以我设法更新此代码以使用cordova 2.3.0(尚未测试与更新版本的cordova的兼容性),享受:
的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" />