Android 如何防止广告拦截器拦截应用上的广告

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3452907/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 10:15:37  来源:igfitidea点击:

How to prevent ad blocker from blocking ads on an app

androidadsadblock

提问by Christopher Perry

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. They told me this mockingly, as if I can't do anything about it.

我的一个用户把猫从包里拿出来,告诉我他们正在使用我的一个免费应用程序,该应用程序通过广告获利,但他们使用广告拦截器阻止了广告。他们嘲笑地告诉我这些,好像我对此无能为力。

Can I do something about it? Is there a way to detect that ads are being blocked?

我可以做些什么吗?有没有办法检测广告被屏蔽了?

回答by Ivan

I am aware of one way that ad blocking works (on any computer really), they edit the hosts file to point to localhost for all known ad servers. For android this is located in the "etc/hosts" file.

我知道广告拦截的一种工作方式(实际上在任何计算机上),他们编辑主机文件以指向所有已知广告服务器的 localhost。对于 android,它位于“etc/hosts”文件中。

For example, I use admob ads and a host file that I have taken from custom rom lists the folowing admob entries:

例如,我使用 admob 广告和我从自定义 rom 中获取的主机文件列出了以下 admob 条目:

127.0.0.1 analytics.admob.com
127.0.0.1 mmv.admob.com
127.0.0.1 mm.admob.com
127.0.0.1 admob.com
127.0.0.1 a.admob.com
127.0.0.1 jp.admob.com
127.0.0.1 c.admob.com
127.0.0.1 p.admob.com
127.0.0.1 mm1.vip.sc1.admob.com
127.0.0.1 media.admob.com
127.0.0.1 e.admob.com

Now anytime a process tries to resolve the above addresses they are routed to the address listed to the left of them (localhost) in this case.

现在,无论何时进程尝试解析上述地址,在这种情况下,它们都会被路由到它们左侧列出的地址 (localhost)。

What I do in my apps is check this host file and look for any admob entries, if I find any I notify the user that I've detected ad blocking and tell them to remove admob entries from there and do't allow them use of the app.

我在我的应用程序中做的是检查这个主机文件并查找任何 admob 条目,如果我找到任何我通知用户我已经检测到广告阻止并告诉他们从那里删除 admob 条目并且不允许他们使用应用程序。

After all what good does it do me if they're not seeing ads? No point in letting them use the app for free.

毕竟,如果他们看不到广告,这对我有什么好处?让他们免费使用该应用程序毫无意义。

Here is a code snippet of how I achieve that:

这是我如何实现这一目标的代码片段:

        BufferedReader in = null;

    try 
    {
        in = new BufferedReader(new InputStreamReader(
                new FileInputStream("/etc/hosts")));
        String line;

        while ((line = in.readLine()) != null)
        {
            if (line.contains("admob"))
            {
                result = false;
                break;
            }
        }
    } 

I vow that all ad supported apps should check this file. You do not need to be root in order to access it, but writing to it might be a different story.

我发誓所有支持广告的应用都应该检查这个文件。您无需成为 root 即可访问它,但写入它可能是另一回事。

Also, not sure if there is any other files that act the same on a linux based OS, but at any rate we can always check all of those files.

另外,不确定在基于 linux 的操作系统上是否还有其他相同的文件,但无论如何我们总是可以检查所有这些文件。

Any suggestions on improving this are welcome.

欢迎提出任何改进此问题的建议。

Also the app called "Ad Free android" needs root access, meaning that it most likely changes the hosts file in order to achieve its goal.

此外,名为“Ad Free android”的应用程序需要 root 访问权限,这意味着它很可能会更改主机文件以实现其目标。

回答by Jonathan

My code for this issue is thusly: -

因此,我针对此问题的代码是:-

try {
    if (InetAddress.getByName("a.admob.com").getHostAddress().equals("127.0.0.1") ||
        InetAddress.getByName("mm.admob.com").getHostAddress().equals("127.0.0.1") ||
        InetAddress.getByName("p.admob.com").getHostAddress().equals("127.0.0.1") ||
        InetAddress.getByName("r.admob.com").getHostAddress().equals("127.0.0.1")) {
        //Naughty Boy - Punishing code goes here.
        // In my case its a dialog which goes to the pay-for version 
        // of my application in the market once the dialog is closed.
    }
} catch (UnknownHostException e) { } //no internet

Hope that helps.

希望有帮助。

回答by BitBank

As developers, we need to do the difficult job of empathizing with the users and find a middle ground between punishing the few who try to take advantage and the many who play by the rules. Mobile advertising is a reasonable way to allow someone to use a functional piece of software for free. The users who employ ad blocking techniques could be considered lost revenue, but if you take a look at the big picture, can also be those who spread the word about your application if they like it. A more gentle approach to running on systems with ads blocked is to display your own "house" ad. Create one or more banner images and display them in the same spot as your normal ad with an ImageViewof the same height (e.g. 50dp). If you successfully receive an ad, then set your ImageView's visibility to View.GONE. You can even create a timer to cycle through several house ads to get the user's attention. Clicking on your ad can take the user to the market page to buy the full version.

作为开发人员,我们需要完成与用户产生共鸣的艰巨工作,并在惩罚少数试图利用规则的少数人和许多遵守规则的人之间找到一个中间立场。移动广告是一种允许某人免费使用功能性软件的合理方式。使用广告拦截技术的用户可能会被视为收入损失,但如果你从大局来看,也可能是那些喜欢你的应用程序的人。在屏蔽广告的系统上运行的一种更温和的方法是显示您自己的“自家”广告。创建一个或多个横幅图像,并使用ImageView将它们显示在与普通广告相同的位置相同高度(例如 50dp)。如果您成功收到广告,请将您的 ImageView 的可见性设置为 View.GONE。您甚至可以创建一个计时器来循环播放多个自家广告,以引起用户的注意。点击您的广告可以将用户带到市场页面购买完整版。

回答by Julian Fondren

The top two answers help you with only a particular (if, probably, the most popular) method of blocking ads. Root users can also block ads with a firewall on the device. WiFi users can block ads with an upstream firewall.

前两个答案仅可帮助您使用一种特定的(如果可能是最流行的)阻止广告的方法。根用户还可以通过设备上的防火墙阻止广告。WiFi 用户可以使用上游防火墙阻止广告。

I suggest:

我建议:

  1. Don't reward ad-blocking users.Ensure that your layout reserves part of the display for an ad even if one can't be loaded. Or if you have a full-screen ad that plays for a bit, ensure that your app waits for a bit even if the ad can't be played. If you use notifications as adverts (you scum), notify the user when you fail to get such an advert. This could be read as "annoy allof your users", but your normal users know what they're getting, and your ad-blocking 'users' aren't wanted.

  2. Ask ad-blockers to stop.The less proftable an industry that supplies what a user wants, the less that industry will supply what the user had wanted. An individual developer will find that he makes more money serving other users. You know this, and your users will think it obvious after you tell them, but it's still an economic argument - it's not intuitive. Have a backup ad that says something like, "This is my job. If you don't pay me, I'll get another one, and you won't get more apps like this from me."

  1. 不要奖励屏蔽广告的用户。确保您的布局为广告保留部分显示内容,即使无法加载。或者,如果您的全屏广告会播放一段时间,请确保即使广告无法播放,您的应用也会等待一段时间。如果您将通知用作广告(您是人渣),请在您无法收到此类广告时通知用户。这可以理解为“惹恼了您的所有用户”,但您的普通用户知道他们得到了什么,并且不需要您的广告拦截“用户”。

  2. 要求广告拦截器停止。提供用户想要的东西的行业利润越低,该行业提供用户想要的东西的利润就越少。个人开发者会发现他为其他用户服务赚了更多的钱。你知道这一点,你的用户在你告诉他们之后会认为这是显而易见的,但这仍然是一个经济论点——它不直观。有一个备用广告,上面写着“这是我的工作。如果你不付钱给我,我会再买一个,你不会从我这里得到更多这样的应用程序。”

回答by Byron Whitlock

Can you check to see if the ad loaded in your app?

您能检查一下您的应用中是否加载了广告吗?

Ad blockers work by preventing your app from downloading data. You could check the content length of the data in your ad frame to make sure there is data there.

广告拦截器的工作原理是阻止您的应用下载数据。您可以检查广告框架中数据的内容长度,以确保那里有数据。

If there is no data throw up a message and exit or warn you with an email.

如果没有数据,则抛出一条消息并退出或通过电子邮件警告您。

It might not be as big an issue as you think since only a small percentage of people block ads.

这可能不像您想象的那么大,因为只有一小部分人会屏蔽广告。

回答by Filipe Pina

Rather than checking for individual software installed or modified hosts file, my approach is using an AdListener like thisand, 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.

而不是检查安装或修改hosts文件单独的软件,我的做法是使用AdListener的这样,如果广告失败,因为加载NETWORK_ERROR,我就去拿一些随机永远在线页面(踢,apple.com)并检查页面是否加载成功。

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 的 Activity 都会执行以下操作:

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();
            }
        });

回答by John

There are two ways for a user to by pass a advertisement:

用户可以通过两种方式绕过广告:

1) Use app without internet on.

1)在没有互联网的情况下使用应用程序。

2) With rooted phone and modified host file.

2)使用root手机和修改过的主机文件。

I made two tools that you can implement, see code below.

我制作了两个可以实现的工具,请参阅下面的代码。

checkifonline(); is for problem 1:

checkifonline(); 是针对问题 1:

public void checkifonline() {
    boolean haveConnectedWifi = false;
    boolean haveConnectedMobile = false;

    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo[] netInfo = cm.getAllNetworkInfo();
    for (NetworkInfo ni : netInfo) {
        if (ni.getTypeName().equalsIgnoreCase("WIFI"))
            if (ni.isConnected())
                haveConnectedWifi = true;
        if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
            if (ni.isConnected())
                haveConnectedMobile = true;
    }

    if(haveConnectedWifi==false && haveConnectedMobile==false){

        // TODO (could make massage and than finish();
    }
}

adblockcheck(); is for problem 2

广告块检查();是针对问题 2

private void adblockcheck() {
    BufferedReader in = null;
    boolean result = true;

    try 
    {
        in = new BufferedReader(new InputStreamReader(
                new FileInputStream("/etc/hosts")));
        String line;

        while ((line = in.readLine()) != null)
        {
            if (line.contains("admob"))
            {
                result = false;
                break;
            }
        }
    } catch (UnknownHostException e) { }
      catch (IOException e) {e.printStackTrace();}  

    if(result==false){

        // TODO (could make massage and than finish();

    }
}

回答by nEx.Software

I think it depends on the content provider for the ads. I know the AdMob SDK provides a callback when an ad request fails. I suspect that you might be able to register for this, then check for a connection in the callback - if there is a connection and you did not receive an ad - take note, if it happens more than once or twice, chances are likely your ads are being blocked. I have not worked with the AdSense for Mobile toolset from Google but it wouldn't surprise me if there was a similar callback mechanism.

我认为这取决于广告的内容提供商。我知道 AdMob SDK 会在广告请求失败时提供回调。我怀疑您可以为此注册,然后在回调中检查连接 - 如果有连接并且您没有收到广告 - 请注意,如果它发生不止一次或两次,很可能是您的广告被屏蔽。我没有使用过 Google 的 AdSense 移动广告工具集,但如果有类似的回调机制,我也不会感到惊讶。

回答by Borealid

There is nothing you can do that your users can't do better.

没有什么是您的用户无法做得更好的。

The only thing that comes to mind as remotely effective is to make the ads an inextricable part of the program, so that if they're blocked the user cannot make sense of/interact with the application.

唯一想到的远程有效是使广告成为程序不可分割的一部分,这样如果它们被阻止,用户就无法理解应用程序/与应用程序交互。

回答by Christopher Perry

This is an extension of a previous answer. The user has informed me that the app they are using is called AdFree Android. It can be found on the market. The app says it works by "nullifying requests to known hostnames serving ads."

这是对先前答案的扩展。用户告诉我他们正在使用的应用程序称为 AdFree Android。它可以在市场上找到。该应用程序表示,它的工作原理是“取消对已知主机名提供广告的请求”。

I suggest that if you monetize any of your apps with ads, you check at startup for this program and give the user a nasty message, then terminate your app.

我建议,如果您通过广告通过任何应用获利,请在启动时检查此程序并向用户发送令人讨厌的消息,然后终止您的应用。