繁体   English   中英

如何通过JavaScript /或Cordova插件调用/启动Android onResume?

[英]How to call/initiate Android onResume from JavaScript/ or a Cordova plug-in?

1. Javascript file ------ calls -------> CORDOVA PLUGIN(plugin.java)
2. CORDOVA PLUGIN(plugin.java) file -------calls-------> Android BroadcastReceiver file(Broadcast_Receiver.java)

Broadcast_Receiver.java包含onResume。
如何从Javascript文件调用/启动它?

请参阅底部的示例代码

问题1:我无法合并plugin.java和Broadcast_Receiver.java,因为它们分别扩展了CordovaPlugin和WLDroidGap。

问题2:在Worklight应用程序的页面之间导航时; Broadcast_Receiver.java的onResume永远不会触发,因为onResume在本机Android代码中

我无法实现的可能解决方案:

  1. 使用cordova onResume,即: document.addEventListener("resume", function() { });
  2. 合并plugin.java和Broadcast_Receiver.java

简而言之:如何使用Cordova插件启动android生命周期?

样例代码

Javascript文件调用插件

    //some code
    currentPage.myfunction= function() {
        cordova.exec(Success, Failure, "plugin","someMETHOD", []);
}

plugin.java

public class plugin extends CordovaPlugin
{
//some code

    float value= Broadcast_Receiver.Variable_Name;

//some code
}

Broadcast_Receiver.java

public class roamingadvisor extends WLDroidGap {

//some code

onCreate(){}
onResume(){}
onPause(){}


//some code
}

我正在努力实现的目标
我的应用程序在收到广播即已激活数据后开始计算数据使用量

  1. 我能够计算数据使用量
    一旦按下主屏幕按钮并重新启动我的应用,它就会更新(即在Android中触发本地onResume)

  2. 但是当我继续使用我的应用程序(即在UI屏幕之间导航)时,本机android onResume永远不会触发

要么

我应该使用完全不同的方法吗?

*******R&D *******

我用下面两个链接

https://apache.googlesource.com/cordova-android/+/2.6.x/framework/src/org/apache/cordova/Device.java
https://github.com/apache/cordova-android/blob/master/framework/src/org/apache/cordova/CordovaPlugin.java

并为我的需要编写了以下代码

public class Myclass extends CordovaPlugin {



    IntentFilter intentFilter = new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE");
    boolean multitasking=true;
    BroadcastReceiver mReceiver=null;

    //constructor
    public MyDataUsage(){

    }


    public void initialize(CordovaInterface cordova, CordovaWebView webView) {

        super.initialize(cordova, webView);
        //Device.uuid = getUuid();
        this.initReceiver();
    }

    @Override
    public void onPause(boolean multitasking) {

        super.onPause(multitasking);
        this.cordova.getActivity().unregisterReceiver(this.mReceiver);
    }

    @Override
    public void onResume(boolean multitasking) {

        super.onResume(multitasking);
        this.cordova.getActivity().registerReceiver(this.mReceiver, intentFilter);
    }



    public void onDestroy() {

        this.cordova.getActivity().unregisterReceiver(this.mReceiver);
    }


    private void initReceiver() {

        this.mReceiver=new BroadcastReceiver(){

            @Override
            public void onReceive(Context context, Intent intent) {
                ctb=context;
                if (intent.getAction().equals("android.net.conn.CONNECTIVITY_CHANGE"))
                        {
                //My code


                }



            }
            }

        };

        this.cordova.getActivity().registerReceiver(this.mReceiver, intentFilter);
    }



    public boolean execute(String action, JSONArray arr,
            final CallbackContext callbackcontext) throws JSONException {


        if (action.equals("getDataUsage")) {

            try {
                 //code
                callbackcontext.success(""+dataUsed+ ":" +timeElapsed+":"+StartTime);
            } catch (Exception e) {
                callbackcontext.error("ERROR :");
            }

            return true;
        }

        return false;

    }

}

但是仅当我按HOME键并重新启动我的APP时,仍会调用onPause和onResume

Should i CONCLUDE that " I can not implement ACTIVITY LIFECYCLE while naviagting in my UI/html SCREEN/pages" ???

默认情况下,Cordova应用程序由一个Activity托管一个WebView。 当您浏览网页或更改Web中的内容时,您将不会从Activity获得任何生命周期事件,因为从技术上讲,您仍处于同一Activity的上下文中。

请注意,CordovaPlugin类具有许多Activity生命周期方法的回调,您可以利用它们( https://github.com/apache/cordova-android/blob/master/framework/src/org/apache/cordova/CordovaPlugin.java )。 例如,使用initialize()方法开始计算,并使用onDestroy()方法检测应用程序何时关闭。 您还可以根据要实现的逻辑使用其他CordovaPlugin方法,例如onResume()和onPause()。

暂无
暂无

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

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