开源项目之 AdFree Detector(屏蔽广告)

AdFree Detector 通过改变广告网络的IP地址、本地IP(改变hosts文件),使大部分的广告被屏蔽。

关键类Detector,如下:

public class Detector {

	/*
	 * com.bigtincan.android.adfree/.FreeMe
	 */
	private static final String ADFREE_PACKAGE_NAME = "com.bigtincan.android.adfree";
	private static final String ADFREE_MAIN_ACTIVITY = "com.bigtincan.android.adfree.FreeMe";

	public static boolean isAdFreeInstalled(Context context) { //是否初始化
		PackageManager pm = context.getPackageManager();
		Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
		mainIntent.setClassName(ADFREE_PACKAGE_NAME, ADFREE_MAIN_ACTIVITY);
		mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
		List<ResolveInfo> apps = pm.queryIntentActivities(mainIntent, 0);
		if (apps.size() == 1) {
			return true;
		} else {
			return false;
		}
	}

	public static boolean isHostAltered(Context context, String hostname) { //是否主机已改变
		ConnectivityManager cm = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		if (cm.getActiveNetworkInfo().isConnected()) {
			try {
				/*
				 * 127.0.0.1 analytics.admob.com 127.0.0.1 analytics.live.com
				 * 127.0.0.1 analytics.msn.com 127.0.0.1 c.googleanalitics.net
				 * 127.0.0.1 feedads.googleadservices.com 127.0.0.1
				 * googleads.g.doubleclick.net 127.0.0.1
				 * googleads2.g.doubleclick.net
				 */
				InetAddress addr = null;
				if (hostname != null && hostname.length() > 0) {
					addr = InetAddress.getByName(hostname);
				} else {
					addr = InetAddress.getByName("googleads.g.doubleclick.net");
				}
				if (addr.getHostAddress().equals("127.0.0.1")) {
					return true;
				} else {
					return false;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			return false;
		} else { // no network connection, we dont care
			return false;
		}
	}
}

项目使用它,需要添加的权限:

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

先检测它是否初始化了

boolean flag = Detector.isAdFreeInstalled(context);

确定特定主机是否改变

isAltered = Detector.isHostAltered(context, "analytics.admob.com");


项目下载

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值