从cordova插件调用后台服务方法

时间:2018-03-04 14:41:03

标签: java android cordova ionic-framework phonegap

我希望能够启动服务,或者在后台服务上运行类,但我想访问我的cordova插件类。

目前,我有类似下面的东西,这不是很好,但它的确有效。但是,如果用户将应用程序推到后面,或者关闭应用程序(而不是服务),那么它就会停止运行。

当用户关闭用户界面或退出应用程序时,我需要 $.extend($.jgrid, { showModal: function (h) { // properties of h // w: (jQuery object) The modal element h.w.css({ left: "3%", // new left position of ERROR dialog top: "3%" // new top position of ERROR dialog }); h.w.show(); } }); 继续运行。

MyHttpServer

我知道要在后台运行一个服务,我可以使用下面的代码,我现在做的其他的东西,它的工作原理,但我无法从cordova访问该实例。

public class MyCordovaPlugin extends CordovaPlugin {

    private static final String TAG = "MyCordovaPlugin";

    MyHttpServer httpServer;

    @Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
        if (httpServer == null) {
            httpServer = new MyHttpServer();
        }

        if (action.equals("get-service-stats")) {
            callbackContext.success(httpServer.getStats());
        }
    }
}

是否有一种特殊的方式来实现cordova插件和adnroid后台服务之间的通信?让我们说这个例子// stop just encase its already started context.stopService(new Intent(context, HttpServerService.class)); // start service context.startService(new Intent(context, HttpServerService.class)); 作为一个名为MyHttpServer的方法,如果MyHttpServer在自己的getStats中运行,我怎么能在我的cordova插件中调用它。

所以像这样,这里是插件

Service

这是后台服务器

public class MyCordovaPlugin extends CordovaPlugin {

    private static final String TAG = "MyCordovaPlugin";

    @Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
        if (action.equals("get-service-stats")) {
            // CALL HttpServerService.GETSTATS METHOD HERE
        }
    }
}

1 个答案:

答案 0 :(得分:0)

  

是否存在实现a之间通信的特定方式   cordova插件和adnroid后台服务?让我们说这个   例如,MyHttpServer作为一个方法就可以调用getStats,我该怎么做   如果MyHttpServer自己运行,请在我的cordova插件中调用它   服务

您可以使用this Developers guide实现活动与服务之间的绑定。通过这样做,您将能够从活动中调用服务的方法。

  

此刻,我有类似下面的东西,这不是很好,   但它的确有效。但是,如果用户将应用程序推到后面或关闭   应用程序(不是服务),然后停止运行。

如果您希望它运行,您的服务必须是前台服务,即使您的活动将被停止。您应该看到this example前台服务的实现。您必须知道Oreo has some limitations后台执行。

抱歉我的英文

相关问题