科尔多瓦#Intent-BroadcastReceiver

Ale*_*ste 2 javascript plugins android broadcastreceiver cordova

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

当我的设备横向放置时,效果很好,所以问题出在Intent-BroadcastReceiver上。

所以我需要那个IntentFilter来知道我的HOOKEVENT,然后用那个BroadcastReceiver接收它。

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

我对CordovaActivity和HOOKEVENT进行了一些测试; 更新文本视图。因此,我认为这是CordovaPlugin的问题。

我也尝试做:

CordovaActivity activity = (CordovaActivity) this.cordova.getActivity();
activity.getJs(); 
Run Code Online (Sandbox Code Playgroud)

通常,这可以使我得到对我的活动有效的字符串,但给了我NPE ..

公共类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\";";
      }
   }
};
Run Code Online (Sandbox Code Playgroud)

Ale*_*ste 6

我发现自己是我的问题。

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

webView.sendJavascript("javascript:document.getElementById(\"combi\").innerHTML=\"decroche\";");
Run Code Online (Sandbox Code Playgroud)

getActivity().getApplicationContext().registerReceiver(broadcastReceiver_hook, filter_hook);
Run Code Online (Sandbox Code Playgroud)

这是我的最终插件:

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;");
        }
    }
};
Run Code Online (Sandbox Code Playgroud)

}