2012-02-08 35 views
0

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

谢谢。

回答

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', []);