科尔多瓦#意向广播接收器 [英] Cordova #Intent-BroadcastReceiver

查看:138
本文介绍了科尔多瓦#意向广播接收器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我与一些特定的API(大涧GXV3275电话),这要求工作意向 - 广播接收器二合一破碎机

在我的设备是横向它的工作好,所以问题就来了意图 - 广播接收器

所以,我需要的IntentFilter知道我HOOKEVENT ANS然后与广播接收器接收到。

我只是想知道为什么它甚至不显示警报或根本不工作。
这有可能处理的IntentFilter上CordovaPlugin?随着广播接收器?

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

我也试图做的事:

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

通常让我得到的字符串,在我的活动工作,但给我的NPE。

公共类吐司扩展CordovaPlugin {

 私人字符串的javascript =;公共布尔执行(串动,JSONArray ARGS,CallbackContext callbackContext)抛出JSONException {
    initHookEvent();
    开关(动作){
        案逆转:
            reversedTest();
            返回true;
    }
    返回false;
}私人活动getActivity(){返回this.cordova.getActivity();}私人无效reversedTest(){
   。配置配置= getActivity()getResources()getConfiguration();
   如果(configuration.orientation == Configuration.ORIENTATION_LANDSCAPE){
       webView.sendJavascript(JavaScript的:的document.getElementById(\\组合式\\)。innerHTML的= \\风景\\;);
   }
   webView.sendJavascript(JavaScript的);
}公共无效initHookEvent(){
   IntentFilter的过滤器=新的IntentFilter(com.base.module.phone.HOOKEVENT);
   。getActivity()registerReceiver(广播接收器,过滤器);
}公共广播接收器广播接收器=新的广播接收器(){
   @覆盖
   公共无效的onReceive(上下文的背景下,意图意图){
      webView.sendJavascript(JavaScript的:警报(\\测试\\););
      如果(intent.getBooleanExtra(hookoff,FALSE)){
         JavaScript的=JavaScript的:的document.getElementById(\\COMBI \\)。innerHTML的= \\decroche \\;;
      }
      其他{
         JavaScript的=JavaScript的:的document.getElementById(\\COMBI \\)。innerHTML的= \\raccroche \\;;
      }
   }
};


解决方案

我发现自己我的问题。

我创建仅适用于之后的特定插件。
你只需要:

<$p$p><$c$c>webView.sendJavascript(\"javascript:document.getElementById(\\\"combi\\\").innerHTML=\\\"decroche\\\";\");

  getActivity()getApplicationContext()registerReceiver(broadcastReceiver_hook,filter_hook)。

这是我最后的插件:

 公共类挂钩扩展CordovaPlugin {@覆盖
公共布尔执行(串动,JSONArray ARGS,CallbackContext callbackContext)抛出JSONException {
    initHookEvent();
    返回false;
}
/ **
 *使用来获得当前活动科尔多瓦
 返回:您科尔多瓦活动
 * /
私人活动getActivity(){返回this.cordova.getActivity();}/ **
 *初始化GXV 3275机事件
 *你绝对需要precise getActivity()。getApplicationContext()
 * registerReceiver()之前,否则将不能得到良好的环境。
 * /
公共无效initHookEvent(){
    IntentFilter的filter_hook =新的IntentFilter(com.base.module.phone.HOOKEVENT);
    。getActivity()getApplicationContext()registerReceiver(broadcastReceiver_hook,filter_hook)。
}/ **
 *还需要与GXV 3275机事件广播接收器
 *只需sendJavascript每个案件
 * /!\\ web视图/!\\
 *本机是通过扩展CordovaPlugin创建
 * /
公共广播接收器broadcastReceiver_hook =新的广播接收器(){
    @覆盖
    公共无效的onReceive(上下文的背景下,意图意图){
        如果(intent.getBooleanExtra(hookoff,FALSE)){
            webView.sendJavascript(JavaScript的:的document.getElementById(\\组合式\\)。innerHTML的= \\decroche \\;);
            webView.sendJavascript(JavaScript的:的document.getElementById(\\组合式\\)。style.opacity = 1;);
        }
        其他{
            webView.sendJavascript(JavaScript的:的document.getElementById(\\组合式\\)。innerHTML的= \\raccroche \\;);
            webView.sendJavascript(JavaScript的:的document.getElementById(\\组合式\\)。style.opacity = 1;);
        }
    }
};

}

First of all, I'm working with some specific API ( Grand Stream GXV3275 phone ) which requires that Intent - BroadcastReceiver combo breaker.

When my device is on landscape orientation it works good so the problem came with Intent - BroadcastReceiver.

So I need that IntentFilter to know my HOOKEVENT ans then receive it with that BroadcastReceiver.

I just want to know why it doesn't even show the alert or don't work at all. Is that possible to deal with IntentFilter on CordovaPlugin? With BroadcastReceiver?

I made some test on my CordovaActivity and HOOKEVENT ; updating a text-view. So I assume that's a problem with CordovaPlugin.

I also tried to do:

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

Which normally allow me to get string that works on my activity but gave me NPE..

public class Toast extends 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\";";
      }
   }
};

解决方案

I found myself my problem.

I create a specific plugin only for that after. You just needed to :

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

And

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

Here's my final plugin :

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;");
        }
    }
};

}

这篇关于科尔多瓦#意向广播接收器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆