How to prevent ad blocker from blocking ads on an app

后端 未结 15 1838
囚心锁ツ
囚心锁ツ 2020-12-12 09:10

One of my users let the cat out of the bag and told me they were using one of my free apps, which is monetized by ads, but they were blocking the ads with an ad blocker. The

15条回答
  •  天命终不由人
    2020-12-12 09:36

    For the case when there is no internet connection, I have followed this tutorial and I've build a "network state listener" like so:

    private BroadcastReceiver mConnReceiver = new BroadcastReceiver() 
    {
        @Override
        public void onReceive(Context context, Intent intent) 
        {
            boolean noConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
    
            if (noConnectivity == true)
            {
                Log.d(TAG, "No internet connection");
                image.setVisibility(View.VISIBLE);
            }
            else
            {
                Log.d(TAG, "Interet connection is UP");
                image.setVisibility(View.GONE);
                add.loadAd(new AdRequest());
            }
        }
    };
    
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        //other stuff
        private ImageView image = (ImageView) findViewById(R.id.banner_main);
        private AdView add = (AdView) findViewById(R.id.ad_main);
        add.setAdListener(new AdListener());
    }
    
    @Override
    protected void onResume()
    {
        registerReceiver(mConnReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
        super.onResume();
    }
    
    @Override
    protected void onPause()
    {
        unregisterReceiver(mConnReceiver);
        super.onPause();
    }
    

    registerReceiver and unregisterReceiver have to be called in onResume and onPause respectively, as described here.

    In your layout xml set up the AdView and an ImageView of your own choice, like so:

    
    
    
    

    Now, whenever the internet connection is available the ad will display and when its off the ImageView will pop-up, and vice-versa. This must be done in every activity in which you want ads to display.

提交回复
热议问题