Android 上的 PhoneGap:HTML <-> 之间的信息流很差JS<->插件<->活动
我有一个问题...(真的吗?:-))
我想做什么?
我需要制作一个本机插件来启动 MapActivity(显示 POI、显示行程等) .),当mapView在某些条件下关闭时,我必须加载特定的html页面。
所以:
.HTML 页面与 JS 事件 -> .JS 插件 -> .Java PlugIn -(启动)-> .Java MapActivity
当我的 MapActivity 完成时:
MapActivity 发送 [Status.ok] 或 [message] X-> .Java插件-> .JS回调->加载新页面
问题是什么?
但不幸的是,我有这样的流程:
-> .java 插件执行:{ Launch(MapActivity); return Response }
然后我的地图活动启动,但我的回调已经传递,没有任何数据。
另一个问题:PhoneGap似乎取消了onActivityResult(),无法在MapViewPlugin.java
代码中获取回调数据?
mapview.js - Plugin
在 MapActivity 启动之前调用的 callBack 存在问题。 sleep(2000) 是在显示 MapView 后显示新页面的解决方法。但这不是什么好问题!
MapView.prototype.showItineraryMapView = function(itineraryEncoded, startLat,
startLong, startTitle, startDesc, finishLat, finishLong, finishTitle,
finishDesc, callBack) {
PhoneGap.exec(function() {
sleep(2000);
callBack();
}, function(){} , "MapView", "show_itinerary", [ itineraryEncoded, startLat, startLong,
startTitle, startDesc, finishLat, finishLong, finishTitle,
finishDesc ]);
};
function sleep(ms) {
var dt = new Date();
dt.setTime(dt.getTime() + ms);
while (new Date().getTime() < dt.getTime());
}
MapViewPlugin.java - 插件
@Override
public PluginResult execute(String action, JSONArray data, String callbackId)
{
Log.i("MapViewPlugin", "Début MapViewPlugin");
PluginResult result = null;
if (action.equals(ACTION_SHOW_ITINERARY))
{
Log.i("MapViewPlugin", "showItineraryMapView");
try
{
return this.showItineraryMapView(data.getString(0), data.getInt(1), data.getInt(2), data.getString(3), data.getString(4), data.getInt(5), data.getInt(6), data.getString(7), data.getString(8));
}
catch (JSONException e)
{
e.printStackTrace();
}
}
else
{
result = new PluginResult(Status.INVALID_ACTION);
Log.d("MapViewPlugin", "Invalid action : " + action + " passed");
}
return result;
}
private PluginResult showItineraryMapView(String encoded_itinerary, int startLat, int startLong, String startTitle, String startDesc, int finishLat, int finishLong, String finishTitle, String finishDesc)
{
Log.i("MapViewPlugin", "Start showMapView()");
Intent iMapView = new Intent(ctx, MapViewActivity.class);
iMapView.putExtra(MODE, MODE_ITINERARY);
iMapView.putExtra(ITINERARY_ENCODED, encoded_itinerary);
iMapView.putExtra(START_LAT, startLat);
iMapView.putExtra(START_LONG, startLong);
iMapView.putExtra(START_TITLE, startTitle);
iMapView.putExtra(START_DESC, startDesc);
iMapView.putExtra(FINISH_LAT, finishLat);
iMapView.putExtra(FINISH_LONG, finishLong);
iMapView.putExtra(FINISH_TITLE, finishTitle);
iMapView.putExtra(FINISH_DESC, finishDesc);
Log.i("MapViewPlugin", "Launching intent...");
ctx.startActivity(iMapView);
return new PluginResult(Status.OK);
}
我不知道这是否是一个好的解决方案,我可能采用了错误的方式。有人可以帮助我吗?
这是我的第一个 stackoverflow 问题,因此如果您想要更精确或者我不够精确,请询问我更多详细信息。
谢谢!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这不太正确。以下是一些应该可以帮助您摆脱困境的更改。首先,您需要保存传递到执行中的callbackId。然后在您的执行方法中调用 showItineraryMapView() 但不让它返回 PluginResult 让它返回 void。
在调用 showItineraryMapView() 后,立即执行以下操作:
然后在 showItineraryMapView() 中,您要调用 ctx.startActivityForResult() 而不是 ctx.startActivity(),并立即删除返回新 PluginResult 的行。
然后您需要重写 onActivityResult() 方法。您可以在此处从 MapView 获取信息。然后你最终调用:
其中“myData”是你想要传回JavaScript端的信息,“this.callbackId”是你存储在execute方法中的回调ID。
It's not quite right. Here are a few changes that should get you unstuck. First you'll need to save the callbackId that is passed into execute. Then in your execute method call showItineraryMapView() but don't get it to return a PluginResult have it return void.
Immediately after you make the call to showItineraryMapView() do this:
Then in showItineraryMapView() you want to call ctx.startActivityForResult() instead of ctx.startActivity() and remove the line immediately afterwards that returns a new PluginResult.
Then you'll need to over-ride the onActivityResult() method. This is where you'll get your information from MapView. Then you end up calling:
where "myData" is the information you want to pass back to the JavaScript side and "this.callbackId" is the callback ID you stored in the execute method.