本文介绍了PhoneGap的3.0自定义插件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我把一个插件的应用程序在几个月前与PhoneGap的2.7和它的工作完美。插件基本上打开了用户电话簿,并返回到我的应用程序的用户选择的联系人的详情。

I put together a plugin for an app some months ago with phonegap 2.7 and it worked perfectly. The plugin basically opens up the users phonebook and returns to my app the details of the contact the user selects.

我最近已经升级到PhoneGap的3.0,我想我的插件转换为3.0;不过,我不能让这个插件现在的工作,这一切都3.0 ....这里是我

I have recently upgraded to Phonegap 3.0 and I am trying to convert my plugin to 3.0; However I can't get the plugin to work now that it's all 3.0....here is what I have

ContactView.java

SRC \ COM \ huronasolutions \插件\ 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

\资产\ WWW \ JS \机器人\ 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文件的头部以下

I have the following in the head of my index.html file

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

当我把它在我的code这样

When I call it in my code like this

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

我得到了LogCat中出现以下错误

I get the following error in LogCat

* 09-17 22:09:24.285:E / Web控制台(1679):未捕获的类型错误:不能调用方法'秀'的未定义的  文件:///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是不确定的。

LogCat says contactView is undefined.

有人可以帮忙,我想我跟每一个导游,我可以在网上找到。谢谢

Can someone help, I think I have followed every guide I can find online.Thanks

推荐答案

打电话给你的插件是这样的:

Call your plugin like this:

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文件:

Also make sure you have defined the plugin in your config.xml:

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

这篇关于PhoneGap的3.0自定义插件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 18:10