我需要在我的 Cordova 应用程序中获取 Android 设备的注册电子邮件。我目前有一个 Cordova 3.3.0 应用程序正在运行;我只能在这个地方找到 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 未定义。