2012-02-08 75 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', []);