如何从javascript隐藏原生admob广告?

时间:2012-02-08 09:05:07

标签: android cordova admob

我开发了一个android phonegap应用程序。该应用程序使用原生admob广告代码,广告显示在应用程序的底部。我选择了本机方法而不是javascript集成,因为本机版本允许我有更多选项来在admob网站中进行修改。我的问题:是否可以通过javascript隐藏/取消隐藏admob广告?

感谢。

1 个答案:

答案 0 :(得分:1)

您可以实施一个插件,用于显示/隐藏广告横幅。 这是一个例子:

com.example.AdBanner:

public class AdBannerPlugin extends Plugin {
    public static final String BROADCAST_ACTION_SHOW_AD_BANNER = "com.example.SHOW_AD_BANNER";
    public static final String BROADCAST_ACTION_HIDE_AD_BANNER = "com.example.HIDE_AD_BANNER";
    private static final String ACTION_SHOW_AD_BANNER = "showBanner";
    private static final String ACTION_HIDE_AD_BANNER = "hideBanner";

    /**
     * @see Plugin#execute(String, org.json.JSONArray, String)
     */
    @Override
    public PluginResult execute(final String action, final JSONArray data, final String callbackId) {
        if (ACTION_SHOW_AD_BANNER.equals(action)) {
            final Intent intent = new Intent();
            intent.setAction(BROADCAST_ACTION_SHOW_AD_BANNER);
            this.ctx.getApplicationContext().sendBroadcast(intent);
            return new PluginResult(OK);
        } else if (ACTION_HIDE_AD_BANNER.equals(action)) {
            final Intent intent = new Intent();
            intent.setAction(BROADCAST_ACTION_HIDE_AD_BANNER);
            this.ctx.getApplicationContext().sendBroadcast(intent);
            return new PluginResult(OK);
        } else {
            Log.e(LOG_TAG, "Unsupported action: " + action);
            return new PluginResult(INVALID_ACTION);
        }
    }
}

在您的主要活动中:

private BroadcastReceiver adReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(final Context context, final Intent intent) {
        if (BROADCAST_ACTION_SHOW_AD_BANNER.equals(intent.getAction())) {
            //check if the ad view is not visible and show it 
        } else if (BROADCAST_ACTION_HIDE_AD_BANNER.equals(intent.getAction())) {
            //check if the ad view is visible and hide it
        }
    }
};

@Override
public void onResume() {
    final IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(BROADCAST_ACTION_HIDE_AD_BANNER);
    intentFilter.addAction(BROADCAST_ACTION_SHOW_AD_BANNER);
    registerReceiver(adReceiver, intentFilter);
    super.onResume();
}

@Override
public void onPause() {
    unregisterReceiver(adReceiver);
    super.onPause();
}

在plugins.xml中:

<plugin name="com.example.AdBanner" value="com.example.AdBannerPlugin"/>

现在您可以隐藏javascript中的广告横幅:

cordova.exec(onSuccess, onFail, 'com.example.AdBanner', 'hideBanner', []);