Android Cordova 插件不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26654231/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 12:01:56  来源:igfitidea点击:

Cordova plugins not working

androidcordova

提问by Stephan Bijzitter

Edit for clarification:The <name>you see in some of the files below is not actually part of the files, but it includes the company name which I had to censor.

编辑澄清:<name>下面你在一些文件中看到的不是真正的文件的一部分,但它包括我不得不的公司名称。

Test situation:

测试情况:

  • Cordova 4
  • OS X Yosemite
  • Android 4.4 device (coupled with USB)
  • Using Terminal; not Eclipse
  • 科尔多瓦 4
  • OS X 优胜美地
  • Android 4.4 设备(搭配USB)
  • 使用终端;不是日食

What happens: Using cordova run --releasesuccessfully builds and signs the .apk and the mobile device installs and opens it without issues. But, none of the plugins work. For example:

会发生什么:使用cordova run --release成功构建和签名 .apk,移动设备安装并打开它,没有问题。但是,没有一个插件工作。例如:

    title.on('click', function() {
        if (!navigator.camera) {
            alert('no camera!');
        } else {
            navigator.camera.getPicture(function success(data) {
            // do something
            }, function error(data) {});
        }
    });

Always alerts that there is no camera, which means Cordova did not add a cameraobject to the window.navigatorobject.

始终提示没有摄像头,这意味着 Cordova 没有camerawindow.navigator对象添加对象。

Below is some information about my cordova installation and some xml files, as far as I can tell nothing looks out of place. Note that it is not just the camera that does not work, I just used it as an example.

下面是关于我的cordova 安装和一些xml 文件的一些信息,据我所知,没有什么不合适的。请注意,不仅仅是相机不起作用,我只是以它为例。

Cordova info:

科尔多瓦信息:

Node version: v0.10.29

Cordova version: 4.0.1-nightly.2014.9.29

Config.xml file: 

<?xml version='1.0' encoding='utf-8'?>
<widget id="com.<name>.App" version="0.0.1" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
    <name><name> Mini</name>
    <description>
        De <name> app voor je telefoon
    </description>
    <author email="stephan@<name>.nl" href="https://<name>.nl">
        Het <name> Team
    </author>
    <content src="index.html" />
    <access origin="*" />
    <preference name="SplashScreen" value="screen" />
    <preference name="SplashScreenDelay" value="2000" />
</widget>


Plugins: 

org.apache.cordova.camera,org.apache.cordova.dialogs,org.apache.cordova.file,org.apache.cordova.file-transfer,org.apache.cordova.media,org.apache.cordova.media-capture,org.apache.cordova.splashscreen,org.apache.cordova.vibration

Android platform:

Available Android targets:
----------
id: 1 or "android-19"
     Name: Android 4.4.2
     Type: Platform
     API level: 19
     Revision: 4
     Skins: HVGA, QVGA, WQVGA400, WQVGA432, WSVGA, WVGA800 (default), WVGA854, WXGA720, WXGA800, WXGA800-7in
 Tag/ABIs : default/armeabi-v7a, default/x86

cordova/config.xml

科尔多瓦/config.xml

<?xml version='1.0' encoding='utf-8'?>
<widget id="com.<name>.App" version="0.0.1" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
    <name><name> Mini</name>
    <description>
        De <name> app voor je telefoon
    </description>
    <author email="stephan@<name>.nl" href="https://<name>.nl">
        Het <name> Team
    </author>
    <content src="index.html" />
    <access origin="*" />
    <preference name="SplashScreen" value="screen" />
    <preference name="SplashScreenDelay" value="2000" />
</widget>

cordova/platforms/android/AndroidManifest.xml <

科尔多瓦/平台/安卓/AndroidManifest.xml <

?xml version='1.0' encoding='utf-8'?>
<manifest android:hardwareAccelerated="true" android:versionCode="1" android:versionName="0.0.1" package="com.kaartje2go.App" 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" />
    <uses-permission android:name="android.permission.INTERNET" />
    <application android:hardwareAccelerated="true" android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale" android:label="@string/activity_name" android:launchMode="singleTop" android:name="CordovaApp" android:theme="@android:style/Theme.Black.NoTitleBar" android:windowSoftInputMode="adjustResize">
            <intent-filter android:label="@string/launcher_name">
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    <uses-sdk android:minSdkVersion="10" android:targetSdkVersion="19" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.RECORD_VIDEO" />
    <uses-permission android:name="android.permission.VIBRATE" />
</manifest>

cordova/platforms/android/res/xml/config.xml

科尔多瓦/平台/android/res/xml/config.xml

<?xml version='1.0' encoding='utf-8'?>
<widget id="com.<name>.App" version="0.0.1" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
    <preference name="loglevel" value="DEBUG" />
    <feature name="Camera">
        <param name="android-package" value="org.apache.cordova.camera.CameraLauncher" />
    </feature>
    <feature name="Notification">
        <param name="android-package" value="org.apache.cordova.dialogs.Notification" />
    </feature>
    <feature name="File">
        <param name="android-package" value="org.apache.cordova.file.FileUtils" />
        <param name="onload" value="true" />
    </feature>
    <feature name="FileTransfer">
        <param name="android-package" value="org.apache.cordova.filetransfer.FileTransfer" />
    </feature>
    <feature name="Media">
        <param name="android-package" value="org.apache.cordova.media.AudioHandler" />
    </feature>
    <feature name="Capture">
        <param name="android-package" value="org.apache.cordova.mediacapture.Capture" />
    </feature>
    <feature name="SplashScreen">
        <param name="android-package" value="org.apache.cordova.splashscreen.SplashScreen" />
    </feature>
    <feature name="Vibration">
        <param name="android-package" value="org.apache.cordova.vibration.Vibration" />
    </feature>
    <name><name> Mini</name>
    <description>
        De <name> app voor je telefoon
    </description>
    <author email="stephan@<name>.nl" href="https://<name>.nl">
        Het <name> Team
    </author>
    <content src="index.html" />
    <access origin="*" />
    <preference name="SplashScreen" value="screen" />
    <preference name="SplashScreenDelay" value="2000" />
</widget>

回答by markmarijnissen

Checklist: Is your plugin loaded?

清单:你的插件加载了吗?

  • Does your index.htmlload the cordova.jsscript? (i.e. if you have www/index.htmlthen you can just reference cordova.js. This file is not in the wwwdirectory, but automatically generated by Cordova and inserted in your app)
  • Is the plugin javascript loaded? Use Chrome or Safari to remote debug your android/iOS app.
  • cordova plugin ls: Is your plugin listed?
  • plugins/android.json: Is your plugin listed?
  • Does platforms/android/assets/wwwhave all files? (i.e. a pluginsfolder with scripts, cordova_plugins.jsshould mention your plugin, a platform-specific cordova.js)
  • index.html加载cordova.js脚本了吗?(即如果你有www/index.html那么你可以只引用cordova.js。这个文件不在www目录中,而是由 Cordova 自动生成并插入到你的应用程序中)
  • 插件 javascript 是否已加载?使用 Chrome 或 Safari 远程调试您的 android/iOS 应用程序。
  • cordova plugin ls: 你的插件列出了吗?
  • plugins/android.json: 你的插件列出了吗?
  • 是否platforms/android/assets/www有所有文件?(即plugins带有脚本的文件夹,cordova_plugins.js应提及您的插件,特定于平台的插件cordova.js

Can you listen to the devicereadyevent to check if Cordova initializes at all? (If all scripts are in place)

你能听一下deviceready事件来检查 Cordova 是否初始化了吗?(如果所有脚本都到位)

回答by tfmontague

If your plugins don't work you can also try these steps:

如果您的插件不起作用,您还可以尝试以下步骤:

cordova plugin list;
cordova plugin remove my-cordova-plugin --save;
cordova plugin add my-cordova-plugin --nosave;

Also be aware of the following plugin dependencies:

还要注意以下插件依赖项:

  1. The file, file-transfer, and cameraplugins are all dependent on the compatplugin; so compatneeds to be removed last.
  2. The file-transferplugin is dependent on file, so fileneeds to be removed first.
  1. filefile-transfercamera插件都依赖于compat插件; 所以compat需要最后删除。
  2. file-transfer插件依赖于file,因此file需要先删除。

回答by opyh

Today I learned another cause why this can break: Ensure that your project does not have a second file named cordova.js, no matter where. If the cordova plugin management finds such a file included as tag, it can wrongly assume that your own file's base path is the base path of Cordova's cordova.jsand uses it as base path for loading its own JS files.

今天,我了解了导致此问题中断的另一个原因:确保您的项目没有名为 的第二个文件cordova.js,无论在哪里。如果cordova插件管理发现这样的文件包含为标签,它可以错误地认为你自己的文件的基本路径是Cordova的基本路径,cordova.js并将其用作加载自己的JS文件的基本路径。