Phonegap插件(版本> = 3.0)以检索Android设备的注册电子邮件地址

时间:2014-03-10 09:05:28

标签: cordova phonegap-plugins cordova-plugins android-account

我需要在Cordova App中获取Android设备的注册电子邮件。我目前有一个Cordova 3.3.0应用程序正在运行;我只能在this place找到版本2.5的Cordova插件 此插件与我当前使用的版本不兼容。有人为此提供了更新的插件吗?

如何更新为2.5构建的插件,使其与3.3.0兼容?

该插件实际上需要android.permission.GET_ACCOUNTS

的许可

更新:我对AccountList.java文件执行了以下操作

package com.seltzlab.mobile;

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

import android.accounts.Account;
import android.accounts.AccountManager;
import android.content.Context;

import org.apache.cordova.*;

import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CordovaWebView;
import org.apache.cordova.PluginResult;

public class AccountList extends CordovaPlugin {

private Context ctx;

public PluginResult execute(String action, JSONArray args, String callbackId) {
try {
JSONObject obj = args.getJSONObject(0);
AccountManager am = AccountManager.get(this.ctx);
Account[] accounts;
if (obj.has("type"))
accounts = am.getAccountsByType(obj.getString("type"));
else
accounts = am.getAccounts();
JSONArray res = new JSONArray();
for (int i = 0; i < accounts.length; i++) {
Account a = accounts[i];
res.put(a.name);
}
return new PluginResult(PluginResult.Status.OK, res);
} catch (JSONException e) {
return new PluginResult(PluginResult.Status.JSON_EXCEPTION);
}
}
}

其中,原文是 -

package com.seltzlab.mobile;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.accounts.Account;
import android.accounts.AccountManager;
import com.phonegap.api.Plugin;
import com.phonegap.api.PluginResult;

public class AccountList extends Plugin {   

@Override   public PluginResult execute(String action, JSONArray args, String callbackId)
 {      
try {           
JSONObject obj = args.getJSONObject(0);         
AccountManager am = AccountManager.get(this.ctx);           
Account[] accounts;         
if (obj.has("type"))                
accounts = am.getAccountsByType(obj.getString("type"));         
else                
accounts = am.getAccounts();            
JSONArray res = new JSONArray();            
for (int i = 0; i < accounts.length; i++) {             
Account a = accounts[i];                
res.put(a.name);            
}           
return new PluginResult(PluginResult.Status.OK, res);       
} 
catch (JSONException e) {           
return new PluginResult(PluginResult.Status.JSON_EXCEPTION);        
}   
}}

我的HTML文件中的脚本如下 -

  <script type="text/javascript" charset="utf-8" src="accountlist.js"></script>

        <script type="text/javascript" charset="utf-8">
        function getAccs(){
        alert("get the Accounts function starts")

        cordova.define("com.seltzlab.mobile.AccountList")

        window.plugins.AccountList.get(
                {
                    type: 'gmail' // if not specified get all accounts
                }, 
                function (result) {
                    alert(res.length);
            for (i in res)
                alert(res[i]);
                },
                function (error) {
                    alert(error);
                }
            );

        }
        </script>

我收到一条错误消息,指出AccountList未定义。

1 个答案:

答案 0 :(得分:0)

尝试联系cordova插件,从Android设备中检索注册的电子邮件地址

https://github.com/apache/cordova-plugin-contacts

例如

function onSuccess(contacts) {
    alert('Found ' + navigator.contacts.length + ' navigator.contacts.');
};

function onError(contactError) {
    alert('onError!');
};

// find all contacts with 'Bob' in any name field
var options      = new ContactFindOptions();
options.filter   = "Bob";
options.multiple = true;
var fields       = ["displayName", "name","email"];
navigator.contacts.find(fields, onSuccess, onError, options);