Android 如何称呼;公共字符串名称(上下文)";在java类中
我正在为Worklight编写本机android代码插件,它看起来像:Android 如何称呼;公共字符串名称(上下文)";在java类中,android,cordova,ibm-mobilefirst,android-context,Android,Cordova,Ibm Mobilefirst,Android Context,我正在为Worklight编写本机android代码插件,它看起来像: public class Getimeiplugin extends CordovaPlugin { @Override public boolean execute(String action, JSONArray args, final CallbackContext callbackContext) throws JSONException { if (ac
public class Getimeiplugin extends CordovaPlugin {
@Override
public boolean execute(String action, JSONArray args, final CallbackContext callbackContext)
throws JSONException {
if (action.equals("getimeiand")){
try {
String Strgetimei = getemei(); ///How to call public String get imei here
final String responseText = Strgetimei + args.getString(0);
cordova.getThreadPool().execute(new Runnable() {
public void run() {
callbackContext.success(responseText); // Thread-safe.
}
});
} catch (JSONException e){
callbackContext.error("Failed to parse parameters");
}
return true;
}
return false;
}
public String getemei(Context context)
{
TelephonyManager mTelephonyMgr;
mTelephonyMgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
String imei = mTelephonyMgr.getDeviceId();
return imei;
}
}
我不知道如何调用
公共字符串getimei(上下文)
,请任何人帮助我?尝试更改此行:
String Strgetimei = getemei();
final String responseText = Strgetimei + args.getString(0);
为此:
String strGetimei = getemei(this.cordova.getActivity().getApplicationContext());
final String responseText = strGetimei + args.getString(0);
或者这个:
String strGetimei = getemei(callBackContext);
我认为这两种方法中的一种会起作用
然后,您必须更改此行:
String Strgetimei = getemei();
final String responseText = Strgetimei + args.getString(0);
为此:
String strGetimei = getemei(this.cordova.getActivity().getApplicationContext());
final String responseText = strGetimei + args.getString(0);
一定要花些时间阅读变量命名约定。您不应该以大写字母开始命名变量。这是为类保留的。它说getApplicationContext()方法对于getImeiPlugin类型定义不足,请检查编辑。我刚刚查阅了stack overflow如何从cordova插件类访问应用程序上下文。添加了第二次编辑也…让我知道,如果这两个工作之一。。。如果没有,我们也会继续挖掘,其他类会调用getemei方法吗?如果没有,您可能应该将其更改为私有方法。现在工作很好,非常感谢,您如此热情,看起来我应该学习更多基本的java语法:(