简体   繁体   English

如何从Cordova插件中覆盖与Android生命周期相关的功能

[英]How to override Android lifecycle related functions from within a Cordova plugin

Adding a native functionality from a regular Android app into my Cordova plugin I found that I need to do: 我发现我需要做的事是将常规Android应用程序的本机功能添加到我的Cordova插件中:

 @Override
    protected void onResume() {
        super.onResume();
        bindService(Intents.getComponentIntent(Intents.COMPONENT_POYNT_SECOND_SCREEN_SERVICE),
                secondScreenServiceConnection, BIND_AUTO_CREATE);
    }

The problem is that I can't copy that on my class that extends CordovaPlugin since the method onResume is not in there: 问题是我无法在扩展CordovaPlugin的类上复制它,因为方法onResume不在其中:

public class MyThing extends CordovaPlugin {
  //MY CODE, THERE IS NO METHOD onResume in CordovaPlugin class
}

Doing some research I was not able to find much information, here they propose extending the ServiceConnection class, but that is beyond my limited understanding of the Android native development. 做一些研究后,我找不到很多信息, 在这里他们建议扩展ServiceConnection类,但这超出了我对Android本机开发的有限理解。

I know Cordova expose some callbacks in the javascript side for the application lifecycle, I could create a hook in my plugin to react from a JS call when onResume happens, BUT I need to execute bindService which is a function not available in my class... Is there maybe another way to execute that? 我知道Cordova在应用程序生命周期的javascript端公开了一些回调,我可以在插件中创建一个钩子以在onResume发生时从JS调用做出反应,但是我需要执行bindService这是我的课程中没有的函数。 。也许还有另一种执行方法?

There is an abstract onResume() method in the CordovaPlugin class which you can override like this: CordovaPlugin类中有一个抽象的onResume()方法 ,您可以像这样重写:

@Override
public void onResume(boolean multitasking) {
    super.onResume(multitasking);
    // Do your stuff here
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM