0

数か月前に 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): Uncaught TypeError: 未定義のメソッド 'show' を 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 が未定義であると言います。

オンラインで見つけることができるすべてのガイドに従っていると思います。ありがとう

4

1 に答える 1

2

次のようにプラグインを呼び出します。

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>
于 2013-09-17T23:36:54.760 に答える