Android 上的 PhoneGap:HTML <-> 之间的信息流很差JS<->插件<->活动

发布于 2024-12-19 07:34:46 字数 3332 浏览 1 评论 0 原文

我有一个问题...(真的吗?:-))

我想做什么?

我需要制作一个本机插件来启动 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

代码中获取回调数据?

ma​​pview.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 问题,因此如果您想要更精确或者我不够精确,请询问我更多详细信息。

谢谢!

I've got a problem... (Really? :-) )

What I want to do?

I need to make a native plugin to launch a MapActivity (to show POI, show itineraries, ...), and when the mapView is closed with some conditions, I have to load a particular html page.

So :

.HTML Page with JS Events -> .JS PlugIn -> .Java PlugIn -(launch)-> .Java MapActivity

And when my MapActivity is finished :

MapActivity send [Status.ok] or [message] X-> .Java PlugIn -> .JS CallBack -> Load new page

What's the problem?

But unfortunately, I have this flow :

-> .java Plugin Execute : { Launch(MapActivity); return Response }

and then my mapactivity is launched, but my callback is already passed without any data.

Another problem : PhoneGap seem's to cancel the onActivityResult(), it's impossible to get callback datas in the MapViewPlugin.java

The code?

mapview.js - Plugin

Problem with callBack which is called before the MapActivity is launched.
The sleep(2000) is a workaround to show the new page after the MapView is shown. But it's not a good issue!

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 - Plugin

@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);
}

I don't know if it's the good solution, I am probably in the wrong way. Can anybody help me?

It's my first stackoverflow question so if you want more precision or if I am not enough precise, please ask me more details.

Thank's!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凑诗 2024-12-26 07:34:46

这不太正确。以下是一些应该可以帮助您摆脱困境的更改。首先,您需要保存传递​​到执行中的callbackId。然后在您的执行方法中调用 showItineraryMapView() 但不让它返回 PluginResult 让它返回 void。

在调用 showItineraryMapView() 后,立即执行以下操作:

PluginResult r = new PluginResult(PluginResult.Status.NO_RESULT);
r.setKeepCallback(true);
return r;

然后在 showItineraryMapView() 中,您要调用 ctx.startActivityForResult() 而不是 ctx.startActivity(),并立即删除返回新 PluginResult 的行。

然后您需要重写 onActivityResult() 方法。您可以在此处从 MapView 获取信息。然后你最终调用:

this.success(new PluginResult(PluginResult.Status.OK, myData), this.callbackId);

其中“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:

PluginResult r = new PluginResult(PluginResult.Status.NO_RESULT);
r.setKeepCallback(true);
return r;

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:

this.success(new PluginResult(PluginResult.Status.OK, myData), this.callbackId);

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文