Java 启动外部意图在Cordova 1.6上有效,但在3.0 Android上无效
我一直在开发一个Cordova Phonegap应用程序,可以启动外部活动。当使用Cordova 1.6时,一切正常,但当我使用Cordova 3.0时,插件将无法工作,测试时,我也不会在logcat上收到任何输出消息。请记住,cordova-1.6.jar中的某些类名与cordova-3.0.jar中的类名不同,因此在实现代码时,导入和某些方法会有一些小的更改。下面是我使用Cordova 3.0版的完整代码: java插件java类 调用插件的startapp.jsJavascript 这里是我定义插件的地方 plugins.xml定义插件Cordova 1.6Java 启动外部意图在Cordova 1.6上有效,但在3.0 Android上无效,java,javascript,android,phonegap-plugins,cordova-3,Java,Javascript,Android,Phonegap Plugins,Cordova 3,我一直在开发一个Cordova Phonegap应用程序,可以启动外部活动。当使用Cordova 1.6时,一切正常,但当我使用Cordova 3.0时,插件将无法工作,测试时,我也不会在logcat上收到任何输出消息。请记住,cordova-1.6.jar中的某些类名与cordova-3.0.jar中的类名不同,因此在实现代码时,导入和某些方法会有一些小的更改。下面是我使用Cordova 3.0版的完整代码: java插件java类 调用插件的startapp.jsJavascript 这里是
插件java代码被调用了吗?你能在里面放一个log语句,看看它是否被调用吗?你好,Mooreds,我在java插件类StartApp.java中添加了一个log语句,但在LogCat中没有得到任何输出。我要做的是发布Cordova 1.6的代码,它的工作方式就像一个魔咒,看看它们的区别!StartApp的原始作者:对不起,我目前没有做任何Android开发,最近安装了新的操作系统,所以我现在甚至没有安装模拟器。不管怎样,您是否碰巧在日志中看到一些JavaScript错误?cordova.exec甚至被执行了吗?可能会在cordova.exec之前/之后添加一些JS日志记录甚至警报。这个调用的返回值是多少?你好,dmedvinsky,我很高兴地说我找到了解决方案,并重新编写了插件类以适应Cordova 3.0.0 API。。我将很快在github上发布回购协议,供大家分享;你有什么最新消息吗
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>
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;}