【问题标题】:Phonegap 3.0 Custom PluginPhonegap 3.0 自定义插件
【发布时间】:2013-09-17 21:26:04
【问题描述】:

几个月前,我用 phonegap 2.7 为一个应用程序编写了一个插件,它运行良好。该插件基本上会打开用户电话簿并将用户选择的联系人的详细信息返回到我的应用程序。

我最近升级到 Phonegap 3.0,我正在尝试将我的插件转换为 3.0;但是我现在无法让插件工作,因为它都是 3.0....这就是我所拥有的

ContactView.java

src\com\huronasolutions\plugins\ContactView.java

package com.huronasolutions.plugins;

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

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.PluginResult;
import org.apache.cordova.CordovaPlugin;

public class ContactView extends CordovaPlugin {
    private static final int PICK_CONTACT = 1;
    private CallbackContext callback;

    @Override
    public boolean execute(String action, JSONArray args, final CallbackContext callbackContext) throws JSONException {
        if (action.equals("getContact")) {

            this.callback = callbackContext;
            cordova.getActivity().runOnUiThread(new Runnable() {
                        public void run() {
                    startContactActivity();
                    PluginResult mPlugin = new PluginResult(PluginResult.Status.NO_RESULT);
                    mPlugin.setKeepCallback(true);
                    callbackContext.sendPluginResult(mPlugin);
                    }
            });
            return true;
        }
        return false;
    }

    public void startContactActivity() {
        Intent intent = new Intent(Intent.ACTION_PICK);
        intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
        this.cordova.startActivityForResult((CordovaPlugin) this, intent,
                PICK_CONTACT);

    }

    @Override
    public void onActivityResult(int reqCode, int resultCode, Intent data) {
        String name = null;
        String number = null;
        String email = null;
        switch (reqCode) {
        case (PICK_CONTACT):
            if (resultCode == Activity.RESULT_OK) {
                Uri contactData = data.getData();
                Cursor c = this.cordova.getActivity().getContentResolver()
                        .query(contactData, null, null, null, null);
                if (c.moveToFirst()) {
                    String ContactID = c.getString(c
                            .getColumnIndex(ContactsContract.Contacts._ID));
                    String hasPhone = c
                            .getString(c
                                    .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));

                    if (Integer.parseInt(hasPhone) == 1) {
                        Cursor phoneCursor = this.cordova
                                .getActivity()
                                .getContentResolver()
                                .query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                                        null,
                                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                                + "='" + ContactID + "'", null,
                                        null);
                        while (phoneCursor.moveToNext()) {
                            number = phoneCursor
                                    .getString(phoneCursor
                                            .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                        }
                    }
                    // get email address
                    Cursor emailCur = this.cordova
                            .getActivity()
                            .getContentResolver()
                            .query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,
                                    null,
                                    ContactsContract.CommonDataKinds.Email.CONTACT_ID
                                            + "='" + ContactID + "'", null,
                                    null);
                    while (emailCur.moveToNext()) {
                        // This would allow you get several email addresses
                        // if the email addresses were stored in an array
                        email = emailCur
                                .getString(emailCur
                                        .getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                        // String emailType = emailCur.getString(
                        // emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
                    }
                    emailCur.close();

                    name = c.getString(c
                            .getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
                    JSONObject contactObject = new JSONObject();
                    try {
                        contactObject.put("name", name);
                        contactObject.put("phone", number);
                        contactObject.put("email", email);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                    callback.success(contactObject);
                }
            }
            break;
        }
    }
}

ContactView.js

\assets\www\js\android\ContactView.js

cordova.define("com.huronasolutions.plugins.ContactView", function (require, exports, module) {
    var exec = require("cordova/exec");

    var contactView = {
        show: function (successCallback, failCallback) {

            function success(args) {
                if (typeof successCallback === 'function')
                    successCallback(args);
            }

            function fail(args) {
                if (typeof failCallback === 'function')
                    failCallback(args);
            }

            return exec(
                function (args) { success(args); },
                function (args) { fail(args); },
                'ContactView',
                'getContact',
                []);
        }
    }
    module.exports = contactView;

});

我的 index.html 文件的头部有以下内容

<script type="text/javascript" src="js/android/ContactView.js"></script>

当我像这样在我的代码中调用它时

window.contactView.show(
               function (contact) {
                   success({ "contact": contact, "msg": "success" });
               },
               function (fail) {
                   fail({ "msg": "We were unable to get the contact you selected." });
               }
           );

我在 LogCat 中收到以下错误

*09-17 22:09:24.285:E/Web 控制台(1679):未捕获的类型错误:无法调用未定义的“显示”方法 file:///android_asset/www/js/txt2.js:477*

当我这样称呼它时

 contactView.show(
                   function (contact) {
                       success({ "contact": contact, "msg": "success" });
                   },
                   function (fail) {
                       fail({ "msg": "We were unable to get the contact you selected." });
                   }
               );

LogCat 说 contactView 未定义。

有人可以帮忙吗,我想我已经遵循了我可以在网上找到的所有指南。 谢谢

【问题讨论】:

    标签: javascript android cordova phonegap-plugins cordova-3


    【解决方案1】:

    像这样调用你的插件:

    cordova.require("com.huronasolutions.plugins.ContactView").show(
     function (contact) {
       success({ "contact": contact, "msg": "success" });
     },
     function (fail) {
       fail({ "msg": "We were unable to get the contact you selected." });
     }
    );
    

    还要确保您已在 config.xml 中定义了插件:

    <feature name="contactView">
          <param name="android-package" value="com.huronasolutions.plugins.ContactView"/
    </feature>
    

    【讨论】:

    • 你是男人!!!!!!!!!!!!谢谢一百万,这就像做梦一样。那么是否有任何捷径可以做到这一点,比如将 cordova.require("com.huronasolutions.plugins.ContactView") 从插件中分配给一个变量?
    • 酷,很高兴它起作用了 :-) 你应该可以快捷方式,因为 cordova.require() 返回一个对象实例,即window.contactView = cordova.require("com.huronasolutions.plugins.ContactView"); window.contactView.show(...);
    • 我可以从插件中执行此操作吗?如果是的话,你会推荐哪里。
    • 我遇到了另一个问题,我可能应该提出一个不同的问题,但我想我会先问你,似乎消息从 java 传递到 javascript 的方式在 3.0 中发生了变化。当我从通讯录中选择联系人时,函数 (contact) { success({ "contact": contact, "msg": "success" }); 中的“联系人”对象} 是“OK”而不是我从 Java 代码传递给回调的 JSONObject。知道为什么以及如何获取数据吗?
    • 我想通了,我不应该在执行方法中有 callback.succes() ......谢谢
    猜你喜欢
    • 2013-08-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-26
    • 1970-01-01
    相关资源
    最近更新 更多