科尔多瓦#Intent-BroadcastReceiver

Cordova #Intent-BroadcastReceiver

首先,我正在使用一些特定的 API(Grand Stream GXV3275 phone),它需要 Intent - BroadcastReceiver 组合断路器。

当我的设备处于横向模式时它运行良好,所以问题出现在 Intent - BroadcastReceiver 上。

所以我需要 IntentFilter 知道我的 HOOKEVENT ans 然后用 BroadcastReceiver 接收它。

我只想知道为什么它甚至不显示警报或根本不工作。 是否可以在 CordovaPlugin 上处理 IntentFilter?使用 BroadcastReceiver?

我对 CordovaActivity 和 HOOKEVENT 做了一些测试;更新文本视图。 所以我认为这是 CordovaPlugin 的问题。

我也试过:

CordovaActivity activity = (CordovaActivity) this.cordova.getActivity();
activity.getJs(); 

这通常允许我获得适用于我 activity 的字符串,但给了我 NPE..

public class Toast 扩展 CordovaPlugin {

private String javascript = "";

public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
    initHookEvent();
    switch (action) {
        case "reversed":
            reversedTest();
            return true;
    }
    return false;
}

private Activity getActivity() { return this.cordova.getActivity();}

private void reversedTest(){
   Configuration configuration = getActivity().getResources().getConfiguration();
   if(configuration.orientation == Configuration.ORIENTATION_LANDSCAPE){
       webView.sendJavascript("javascript:document.getElementById(\"combi\").innerHTML=\"Landscape\";");
   }
   webView.sendJavascript(javascript);
}

public void initHookEvent() {
   IntentFilter filter = new IntentFilter("com.base.module.phone.HOOKEVENT");
   getActivity().registerReceiver(broadcastReceiver, filter);
}

public BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
   @Override
   public void onReceive(Context context, Intent intent) {
      webView.sendJavascript("javascript:alert(\"test\");");
      if (intent.getBooleanExtra("hookoff", false)){
         javascript = "javascript:document.getElementById(\"combi\").innerHTML=\"decroche\";";
      }
      else{
         javascript = "javascript:document.getElementById(\"combi\").innerHTML=\"raccroche\";";
      }
   }
};

我发现自己的问题。

之后我专门为此创建了一个特定的插件。 您只需要:

webView.sendJavascript("javascript:document.getElementById(\"combi\").innerHTML=\"decroche\";");

getActivity().getApplicationContext().registerReceiver(broadcastReceiver_hook, filter_hook);

这是我的最终插件:

public class Hook extends CordovaPlugin {

@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
    initHookEvent();
    return false;
}


/**
 * Use to get the current Cordova Activity
 * @return your Cordova activity
 */
private Activity getActivity() { return this.cordova.getActivity();}

/**
 * Initializing GXV 3275 Hook Event
 * You ABSOLUTELY need to precise getActivity().getApplicationContext()
 * before registerReceiver() otherwise it won't get the good context.
 */
public void initHookEvent() {
    IntentFilter filter_hook = new IntentFilter("com.base.module.phone.HOOKEVENT");
    getActivity().getApplicationContext().registerReceiver(broadcastReceiver_hook, filter_hook);
}

/**
 * BroadcastReceiver is also needed with GXV 3275 Hook Event
 * Just sendJavascript for each cases
 *       /!\ webView /!\
 * Is natively created by extending CordovaPlugin
 */
public BroadcastReceiver broadcastReceiver_hook = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        if ( intent.getBooleanExtra("hookoff", false)){
            webView.sendJavascript("javascript:document.getElementById(\"combi\").innerHTML=\"decroche\";");
            webView.sendJavascript("javascript:document.getElementById(\"combi\").style.opacity = 1;");
        }
        else{
            webView.sendJavascript("javascript:document.getElementById(\"combi\").innerHTML=\"raccroche\";");
            webView.sendJavascript("javascript:document.getElementById(\"combi\").style.opacity = 1;");
        }
    }
};

}