这是一段我自己搞砸的代码。
public PluginResult execute(String action, JSONArray args, String callbackId)
{
PluginResult.Status status = PluginResult.Status.OK;
String result = "";
if (action.equals("sayCordova"))
{
Context context = cordova.getActivity().getApplicationContext(); //Initializing cordova to enjoy its method of getApplication Context but i am not getting how to do so.
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
number = tm.getLine1Number();
if(number.length() !=0 && number == null)
{
System.out.println("Context is "+context+" NO. :: "+number);
result=number;
return new PluginResult(status, result);
}
result="No Number found!!!";
return new PluginResult(status, result);
}
result="Issue with Action data";
return new PluginResult(status, result);
}
我的问题是:
如何初始化和实例化cordova android插件,以便我可以使用它的方法?
答案 0 :(得分:0)
有一种简单的方法可以执行此操作:
@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException
{
if ("sayCordova".equals(action))
{
TelephonyManager tm = (TelephonyManager) this.cordova.getActivity().getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
number = tm.getLine1Number();
callbackContext.success(number);
return true;
}
callbackContext.error("Invalid action "+number);
return false;
}