Cordova 3.1 Oriention not working

2019-05-29 01:33发布

问题:

I have an application (Cordova 3.1) which should only be displayed in portrait.

My config.xml:

<?xml version='1.0' encoding='utf-8'?>
<widget id="de.something.test" version="0.0.6" xmlns="http://www.w3.org/ns/widgets"
    xmlns:cdv="http://cordova.apache.org/ns/1.0">
<name>Something 2.0</name>
<description>
    A sample Apache Cordova application that responds to the deviceready event.
</description>
<author email="dev@callback.apache.org" href="http://cordova.io">
    Apache Cordova Team
</author>
<content src="index.html"/>
<access origin="*"/>
<preference name="fullscreen" value="false"/>
<preference name="webviewbounce" value="true"/>
<preference name="splashscreen" value="splash"/>
<preference name="splashScreenDelay" value="2000"/>
<preference name="DisallowOverscroll" value="true"/>
<preference name="target-device" value="handset"/>
<preference name="detect-data-types" value="false"/>
<preference name="ios-statusbarstyle" value="black-opaque" />
<preference name="Orientation" value="portrait" />
</widget>

The generated AndroidManifest.xml after running ´cordova build android´ in the folder:

<?xml version='1.0' encoding='utf-8'?>
<manifest android:hardwareAccelerated="true" android:versionCode="1" android:versionName="0.0.6" android:windowSoftInputMode="adjustPan" package="de.pfeffermind.spyday" xmlns:android="http://schemas.android.com/apk/res/android">
<supports-screens android:anyDensity="true" android:largeScreens="true" android:normalScreens="true" android:resizeable="true" android:smallScreens="true" android:xlargeScreens="true" />
<application android:debuggable="true" android:hardwareAccelerated="true" android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale" android:label="@string/app_name" android:name="Something20" android:theme="@android:style/Theme.Black.NoTitleBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode" android:hardwareAccelerated="false" android:name="com.flurry.android.FlurryFullscreenTakeoverActivity" />
</application>
<uses-sdk android:minSdkVersion="10" android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
</manifest>

So there is no

<activity android:name=".SomeActivity" android:label="@string/app_name" android:screenOrientation="portrait">

to disable the landscape mode.

Can anyone help here?

Thanks!

Sebastian

回答1:

Cordova locking orientation via config.xml is broken due to platform inconsistency I guess: https://issues.apache.org/jira/browse/CB-5634. I Installed the plugin to workaround it, then in your main module lock the orientation.

    document.addEventListener('deviceready', function(){
        window.plugins.orientationLock.lock("landscape");   
    });


回答2:

See manifest config

activity android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale" android:label="@string/app_name" android:name="Something20" android:theme="@android:style/Theme.Black.NoTitleBar"
//Add in Manifest
android:screenOrientation="portrait">

    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

You can no understand about config.xml in www folder. It only use when you use Phonegap Build to build app. In case, not working in Android emulator.



回答3:

It seems this behaviour isnt automatically implemented for local cordova builds as you can read here (http://docs.phonegap.com/en/3.3.0/guide_platforms_android_config.md.html#Android%20Configuration) and in the source code.

  • You could use a Plugin for this, for example: https://github.com/cogitor/PhoneGap-OrientationLock.
  • You could manipulate the AndroidManifest as Hanh Le stated