用途科尔多瓦申请本地code [英] Uses native code in cordova application
问题描述
我在原生Android后续的code
I have a following code in native android
List<PackageInfo> PackList = getPackageManager().getInstalledPackages(0);
for (int i=0; i < PackList.size(); i++)
{
PackageInfo PackInfo = PackList.get(i);
if ( ( (PackInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) != true)
{
String AppName = PackInfo.applicationInfo.loadLabel(getPackageManager()).toString();
Log.e("App" + Integer.toString(i), AppName);
}
}
用于获取有关移动安装的应用程序的信息。我想在科尔多瓦/ PhoneGap的
应用程序中使用。
- 如何使用?
- 我需要哪些步骤?
- 或者反正是有在不同的平台上同一code使用
(机器人,窗口)?
我还检查了这链接但没'不懂
I also checked this link but didn't understand
在此先感谢
推荐答案
要在你必须创建一个PhoneGap的一个插件应用程序的PhoneGap使用本地code。这实际上是pretty简单:
To use native code in a phonegap app you have to create a phonegap plugin. This is actually pretty simple:
1)创建你的PhoneGap插件类,在这种情况下,插件只响应行动举杯来创建一个原生的Android举杯:
1) Create you phonegap plugin class, in this case the plugin only responds to the action "toast" to create a native Android toast:
public class ExamplePlugin extends CordovaPlugin {
@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
if("toast".equals(action)) {
// The first String in the arguments array is the text for the toast
String text = args.getString(0);
Toast toast = Toast.makeText(this.cordova.getActivity(), text, Toast.LENGTH_SHORT);
toast.show();
// With PluginResult you can send results back to the js layer.
PluginResult result = new PluginResult(PluginResult.Status.OK, "--toast displayed.");
result.setKeepCallback(true);
callbackContext.sendPluginResult(result);
}
// Return true if everything worked as it should. If an error occurs return false.
// Depending on this return value either the success or error callback is invoked.
return true;
}
@Override
public void initialize(CordovaInterface cordova, CordovaWebView webView) {
super.initialize(cordova, webView);
// Here you can perform some initialisations for your plugin.
}
}
然后在你的PhoneGap应用程序的config.xml中添加这一行:
Then in the config.xml of your phonegap app you add this line:
<plugin name="ExamplePlugin" value="com.phonegap.example.ExamplePlugin" />
名称由你可以从JavaScript和值是你的插件的完整的类名称调用插件的名称。
Name being the name by which you can call the plugin from javascript and value being the complete class name of your plugin.
现在你可以从JavaScript调用插件:
Now you can call the plugin from javascript:
cordova.exec(successCallback, errorCallback, "ExamplePlugin", "toast", ["Hello Cordova!!1"]);
您提供两个回调,你要打电话,要执行的动作插件的名称和参数数组,在我们的情况下,插件被命名为ExamplePlugin的动作是敬酒我们只有在字符串参数作为文本为我们敬酒。
You supply two callbacks, the name of the plugin you want to call, the action you want to execute and an array of arguments, in our case the plugin is named "ExamplePlugin" the action is "toast" and we only have on string argument as text for our toast.
当然,这只是工作为Android。如果你想为不同的平台例如相同的功能iOS版的Windows Phone等您去必须得为每一个他们的插件。
Of course this just works for Android. If you want the same functionality for different platforms e.g. iOS, Windows Phone etc. You are gonna have to create a plugin for each of them too.
这篇关于用途科尔多瓦申请本地code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!