PhoneGap屏幕方向的插件更新为2.7.0或2.8.0 [英] Phonegap Screen-orientation plugin update for 2.7.0 or 2.8.0

查看:195
本文介绍了PhoneGap屏幕方向的插件更新为2.7.0或2.8.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人在那里,谁知道如何修复这个插件使用最新的cordova / phonegap版本?



我不知道如何修复eclipse中的错误,我真的需要一种方法来改变我的几个页面的方向

解决方案

您需要进行以下更改:



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 {
//参考http://developer.android.com/reference/android/R.attr.html#screenOrientation
private static final字符串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);
活动活动= 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();
返回true;
} else {
return false;
}
}
}

pg-plugin -screen-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插件示例< / title>
< meta charset =utf-8>
< script type =text / javascriptcharset =utf-8src =cordova.js>< / script>
< script type =text / javascriptcharset =utf-8src =pg-plugin-screen-orientation.js>< / script>
< script type =text / javascript>
函数setOrientation(type){
cordova.require('cordova / plugin / screenorientation')。set(
type,
function(){
console.log (成功设置+类型);
},
function(){
console.error(无法设置+类型);
}
) ;
}
< / script>
< / head>
< body>
< h1> ScreenOrientation PhoneGap插件示例< / h1>
< input type =buttononclick =setOrientation('landscape'); value =Landscape/>
< input type =buttononclick =setOrientation('portrait'); value =Portrait/>
< input type =buttononclick =setOrientation('sensorLandscape'); value =传感器景观/>
< input type =buttononclick =setOrientation('sensorPortrait'); value =传感器肖像/>
< input type =buttononclick =setOrientation('reverseLandscape'); value =Reverse Landscape/>
< input type =buttononclick =setOrientation('reversePortrait'); value =反向人像/>
< input type =buttononclick =setOrientation('fullSensor'); value =全传感器/>
< / body>
< / html>

我把它放在一起作为一个Eclipse项目 - 你可以下载它并生成的APK 这里



注意:您可能需要降级从Cordova 2.8.0到Cordova 2.4.0直到在Cordova 2.5.0+中暂停应用程序的问题已经解决 - 看到这里


Is there anybody out there, who knows how to fix this plugin to work with the newest cordova/phonegap version?

I have no idea how i can fix the errors in eclipse and i really need a method to change orientation for several pages in my phonegap app!

解决方案

You need to make the following changes:

ScreenOrientation.java

Replace with this:

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-plugin-screen-orientation.js

Replace with this:

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

Then you can replace sample.html with this:

<!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>

I've put this together as an Eclipse project - you can download it and the resulting APK here

Note: You might want downgrade from Cordova 2.8.0 to Cordova 2.4.0 until issues with pausing the app in Cordova 2.5.0+ have been resolved - see here

这篇关于PhoneGap屏幕方向的插件更新为2.7.0或2.8.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆