javascript - Cordova #Intent-BroadcastReceiver

标签 javascript android cordova plugins broadcastreceiver

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

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

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

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

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

我也试过:

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

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

公共(public)类 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;");
        }
    }
};

关于javascript - Cordova #Intent-BroadcastReceiver,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29774238/

相关文章:

javascript - nopcommerce 3.80 首屏内容中的 ender-blocking JavaScript 和 CSS

javascript - 用纯css模拟mouseenter

javascript - 文档中的 # 符号是什么意思(示例 : Cache#get(key[, 选项]))

javascript - jQuery - 将垂直菜单更改为水平菜单

android - 在 Release模式下构建 VisualStudio Cordova 应用程序时出现错误

blackberry - PhoneGap 应用程序黑莓(服务器端)中的推送通知

javascript - Phonegap 插件 - module.exports

android - 多个动画的相同插值器

java - 安卓 : How to remove white spaces in Chinese characters?

android - 如何根据设置的主题更改flutter中的状态栏图标和文本颜色?