How to prevent ad blocker from blocking ads on an app

后端 未结 15 1830
囚心锁ツ
囚心锁ツ 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条回答
  •  萌比男神i
    2020-12-12 09:39

    Rather than checking for individual software installed or modified hosts file, my approach is using an AdListener like this and, if the ad fails to load due to NETWORK_ERROR, I just fetch some random always-online page (for the kicks, apple.com) and check if the pages loads successfully.

    If so, boom goes the app.

    To add some code, listener class would be something like:

    public abstract class AdBlockerListener implements AdListener {
    
        @Override
        public void onFailedToReceiveAd(Ad arg0, ErrorCode arg1) {
            if (arg1.equals(ErrorCode.NETWORK_ERROR)) {
                try {
                    URL url = new URL("http://www.apple.com/");
                    URLConnection conn = url.openConnection();
                    BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                    reader.readLine();
                    onAdBlocked();
                } catch (IOException e) {}
            }
        }
    
        public abstract void onAdBlocked();
    }
    

    And then each activity with an adView would do something like:

    AdView adView = (AdView) findViewById(R.id.adView);
            adView.setAdListener(new AdBlockerListener() {
                @Override
                public void onAdBlocked() {
                    AlertDialog ad = new AlertDialog.Builder(CalendarView.this)
                                        .setMessage("nono")
                                        .setCancelable(false)
                                        .setNegativeButton("OK", new OnClickListener() {
                                            public void onClick(DialogInterface dialog, int which) {
                                                System.exit(1);
                                            }
                                        })
                                        .show();
                }
            });
    

提交回复
热议问题