egvrcn
egvrcn

Reputation: 984

When internet connection is not available on Android App, not show AdMob

I want to listen to internet access on Android App,if not available internet hide AdMob Area. By the way my application context are will be wider. If there is internet access, i want to show AdMob and Area. How can I do it? thanks

Upvotes: 0

Views: 671

Answers (2)

William
William

Reputation: 20196

Register the following as a BroadcastReceiver to find out when the internet connection changes. When you have connection you can display the AdView.

private BroadcastReceiver wifiConnectionListener = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {

        final NetworkInfo networkInfo = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
        final boolean isOnline = (networkInfo != null && networkInfo.isConnected());

        Log.d(TAG, "#onReceive wifiNetworkInfo=" + networkInfo);
        Log.d(TAG, "#onReceive online=" + isOnline + " isConnected=" + getMessageSender().isConnected());

        if (isOnline) {
            Log.d(TAG, "#onReceive showing AdView");
            // TODO Show your AdView
        }
    }
};

And then register the BroadcastListsner

getActivity().registerReceiver(wifiConnectionListener, 
      new IntentFilter(WifiManager.NETWORK_STATE_CHANGED_ACTION));

Upvotes: 0

FD_
FD_

Reputation: 12919

That already is the default behaviour of AdMob. You don't have to do anything.

Upvotes: 3

Related Questions