Android - 如何防止一个应用程序被另一个应用程序访问互联网
Android - How to prevent an application from accessing to the internet by another application
TrafficStats 为我们提供有关哪个应用程序消耗带宽的信息。
我想编写一个应用程序,在需要时 disables/enables 其他应用程序的互联网使用情况。
我看到有一些 google 播放应用程序正在这样做,但是怎么做的?
private void setMobileDataEnabled(Context context, boolean enabled) throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
final ConnectivityManager conman = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
final Class conmanClass = Class.forName(conman.getClass().getName());
final Field connectivityManagerField = conmanClass.getDeclaredField("mService");
connectivityManagerField.setAccessible(true);
final Object connectivityManager = connectivityManagerField.get(conman);
final Class connectivityManagerClass = Class.forName(connectivityManager.getClass().getName());
final Method setMobileDataEnabledMethod = connectivityManagerClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
setMobileDataEnabledMethod.setAccessible(true);
setMobileDataEnabledMethod.invoke(connectivityManager, enabled);
}
不要忘记将此行添加到您的清单文件中
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
为此考虑使用 iptables
很好的例子(显示如何编写防火墙程序)是 orWall
Put your apps behind Orbot and block all unwanted traffic in one go.
Orwall page also pointing to GitHub source Codes
"This is not a solution. This is just another program doing this thing. I am asking how to code a program like this.. Which can enable or disable internet access for another application. So -1"
你会读书吗?
iptables 是一个用户-space 应用程序,允许系统管理员配置 Linux 内核防火墙(作为不同的 Netfilter 模块实现)提供的表及其存储的链和规则.不同的内核模块和程序目前用于不同的协议; iptables适用于IPv4,ip6tables适用于IPv6,arptables适用于ARP,ebtables适用于以太网帧。
iptables需要提升权限才能运行,必须由root用户执行,否则无法运行。在大多数 Linux 系统上,iptables 安装为 /usr/sbin/iptables 并记录在其手册页中,可以在安装时使用 man iptables 打开。它也可能在 /sbin/iptables 中找到,但由于 iptables 更像是一种服务而不是 "essential binary",首选位置仍然是 /usr/sbin.
术语 iptables 也常用于泛指内核级组件。 x_tables 是内核模块的名称,包含所有四个模块使用的共享代码部分,还提供用于扩展的 API;随后,Xtables 或多或少被用来指代整个防火墙(v4、v6、arp 和 eb)架构。
iptables的后继者是nftables,在2014年1月19日发布的Linux内核版本3.13中被合并到Linux内核主线
TrafficStats 为我们提供有关哪个应用程序消耗带宽的信息。
我想编写一个应用程序,在需要时 disables/enables 其他应用程序的互联网使用情况。
我看到有一些 google 播放应用程序正在这样做,但是怎么做的?
private void setMobileDataEnabled(Context context, boolean enabled) throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
final ConnectivityManager conman = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
final Class conmanClass = Class.forName(conman.getClass().getName());
final Field connectivityManagerField = conmanClass.getDeclaredField("mService");
connectivityManagerField.setAccessible(true);
final Object connectivityManager = connectivityManagerField.get(conman);
final Class connectivityManagerClass = Class.forName(connectivityManager.getClass().getName());
final Method setMobileDataEnabledMethod = connectivityManagerClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
setMobileDataEnabledMethod.setAccessible(true);
setMobileDataEnabledMethod.invoke(connectivityManager, enabled);
}
不要忘记将此行添加到您的清单文件中
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
为此考虑使用 iptables
很好的例子(显示如何编写防火墙程序)是 orWall
Put your apps behind Orbot and block all unwanted traffic in one go.
Orwall page also pointing to GitHub source Codes
"This is not a solution. This is just another program doing this thing. I am asking how to code a program like this.. Which can enable or disable internet access for another application. So -1"
你会读书吗?
iptables 是一个用户-space 应用程序,允许系统管理员配置 Linux 内核防火墙(作为不同的 Netfilter 模块实现)提供的表及其存储的链和规则.不同的内核模块和程序目前用于不同的协议; iptables适用于IPv4,ip6tables适用于IPv6,arptables适用于ARP,ebtables适用于以太网帧。
iptables需要提升权限才能运行,必须由root用户执行,否则无法运行。在大多数 Linux 系统上,iptables 安装为 /usr/sbin/iptables 并记录在其手册页中,可以在安装时使用 man iptables 打开。它也可能在 /sbin/iptables 中找到,但由于 iptables 更像是一种服务而不是 "essential binary",首选位置仍然是 /usr/sbin.
术语 iptables 也常用于泛指内核级组件。 x_tables 是内核模块的名称,包含所有四个模块使用的共享代码部分,还提供用于扩展的 API;随后,Xtables 或多或少被用来指代整个防火墙(v4、v6、arp 和 eb)架构。
iptables的后继者是nftables,在2014年1月19日发布的Linux内核版本3.13中被合并到Linux内核主线