Cordova #Intent-BroadcastReceiver

柔情痞子 提交于 2019-12-06 15:05:38

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

}

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!