我一直在开发能够启动外部活动的Cordova(Phonegap)应用程序。当使用Cordova 1.6时,一切正常,但是当我使用cordova 3.0时,插件将无法正常工作,并且在测试时我也没有在logcat上获得任何输出消息。请记住,cordova-1.6.jar中的某些类名与cordova-3.0.jar的名称不同,因此在实现代码时,对导入和某些方法进行了一些小的更改。下面是我使用Cordova 3.0版的完整代码:
package com.example.test;
import org.apache.cordova.DroidGap;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.PluginResult;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.content.ComponentName;
import android.content.Intent;
public class StartApp extends CordovaPlugin
{
/**
* Executes the request and returns PluginResult.
*
* @param action
* Action to perform.
* @param args
* Arguments to the action.
* @param callbackId
* JavaScript callback ID.
* @return A PluginResult object with a status and message.
*/
public PluginResult execute(String action, JSONArray args, String callbackId)
{
try {
if (action.equals("startApp")) {
if (args.length() != 1) {
return new PluginResult(PluginResult.Status.INVALID_ACTION);
}
String component = args.getString(0);
startActivity(component);
return new PluginResult(PluginResult.Status.OK);
}
return new PluginResult(PluginResult.Status.INVALID_ACTION);
} catch (JSONException e) {
e.printStackTrace();
return new PluginResult(PluginResult.Status.JSON_EXCEPTION);
}
}
/**
* Starts an activity.
*
* @param component
* Activity ComponentName.
* E.g.: com.mycompany.myapp/com.mycompany.myapp.MyActivity
*/
void startActivity(String component) {
Intent intent = new Intent("android.intent.action.MAIN");
intent.addCategory("android.intent.category.LAUNCHER");
intent.setComponent(ComponentName.unflattenFromString(component));
cordova.getActivity().startActivity(intent);
}
}
var StartApp = function() { };
StartApp.prototype.start = function(params, success, fail) {
success = success ? success : function() {};
fail = fail ? fail : function() {};
var component = params.android;
return cordova.exec(success, fail, 'StartApp', 'startApp', [component]);
};
window.startapp = new StartApp();
<?xml version='1.0' encoding='utf-8'?>
<widget id="io.cordova.helloCordova" version="2.0.0" xmlns="http://www.w3.org/ns/widgets">
<name>Hello Cordova</name>
<description>
A sample Apache Cordova application that responds to the deviceready event.
</description>
<author email="dev@cordova.apache.org" href="http://cordova.io">
Apache Cordova Team
</author>
<content src="index.html" />
<feature name="App">
<param name="android-package" value="org.apache.cordova.App" />
</feature>
<feature name="StartApp">
<param name="android-package" value="com.example.test.StartApp" />
</feature>
<access origin="*" />
<preference name="useBrowserHistory" value="true" />
<preference name="exit-on-suspend" value="false" />
<preference name="fullscreen" value="true" />
<preference name="webviewbounce" value="true" />
</widget>
<body>
<div class="app">
<h1>Apache Cordova</h1>
<div id="deviceready" class="blink">
<p class="event listening">Connecting to Device</p>
<p class="event received">Device is Ready</p>
</div>
<button onclick="launchNow()">Launch</button>
</div>
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="js/index.js"></script>
<script type="text/javascript" charset="utf-8" src="startapp.js"></script>
<script type="text/javascript">
app.initialize();
</script>
<script>
function launchNow(){
window.startapp.start(
{android: 'com.viessmann.etapp/com.viessmann.etapp.activities.SplashScreenActivity'},
successCallback, failureCallback
);
}
function successCallback(){return true;}
function failureCallback(){return true;}
</script>
</body>
有关cordova 3.0.0插件实现的更多参考资料,请访问:
http://cordova.apache.org/docs/en/3.0.0/guide_platforms_android_plugin.md.html#Android%20Plugins
package com.viessmann.launcher;
import org.apache.cordova.DroidGap;
import org.apache.cordova.api.Plugin;
import org.apache.cordova.api.PluginResult;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.content.ComponentName;
import android.content.Intent;
import android.util.Log;
/**
* Launches an external application.
*
* @author Dmitry Medvinsky <dmedvinsky@gmail.com>
* @license MIT/X11
*/
public class StartApp extends Plugin
{
String TAG = "SUCCESS";
/**
* Executes the request and returns PluginResult.
*
* @param action
* Action to perform.
* @param args
* Arguments to the action.
* @param callbackId
* JavaScript callback ID.
* @return A PluginResult object with a status and message.
*/
public PluginResult execute(String action, JSONArray args, String callbackId)
{
try {
if (action.equals("startApp")) {
if (args.length() != 1) {
Log.i(TAG, "CLASS CALLED 1");
return new PluginResult(PluginResult.Status.INVALID_ACTION);
}
String component = args.getString(0);
startActivity(component);
Log.i(TAG, "CLASS CALLED 2");
return new PluginResult(PluginResult.Status.OK);
}
return new PluginResult(PluginResult.Status.INVALID_ACTION);
} catch (JSONException e) {
e.printStackTrace();
Log.i(TAG, "CLASS CALLED 3");
return new PluginResult(PluginResult.Status.JSON_EXCEPTION);
}
}
/**
* Starts an activity.
*
* @param component
* Activity ComponentName.
* E.g.: com.mycompany.myapp/com.mycompany.myapp.MyActivity
*/
void startActivity(String component) {
Intent intent = new Intent("android.intent.action.MAIN");
intent.addCategory("android.intent.category.LAUNCHER");
intent.setComponent(ComponentName.unflattenFromString(component));
this.ctx.startActivity(intent);
}
}
var StartApp = function() { };
StartApp.prototype.start = function(params, success, fail) {
success = success ? success : function() {};
fail = fail ? fail : function() {};
var component = params.android;
return cordova.exec(success, fail, 'StartApp', 'startApp', [component]);
};
window.startapp = new StartApp();
<plugins>
<plugin name="StartApp" value="com.viessmann.launcher.StartApp"/>
</plugins>
function launchNow(){
window.startapp.start(
{android: 'com.viessmann.etapp/com.viessmann.etapp.activities.SplashScreenActivity'},
successCallback, failureCallback
);
}
function successCallback(){return true;}
function failureCallback(){return true;}