Android:仅阻止一个 activity 访问互联网

Android: preventing only a single activity from accessing internet

Android 应用声明它们需要什么权限才能工作,以便用户了解。

假设我的 Android 应用程序声明了互联网许可,但它只是在一个 activity 中使用互联网访问,并且想阻止另一个 activity 访问互联网。

假设不打算访问互联网的 activity 有一个 WebView,它对用户提供的一些 HTML 文件执行一些 javascript 处理:任何黑客攻击都必须被阻止,因此无法向此 activity 或 WebView 本身授予互联网访问权限。

可能吗?

android 框架级别不是像这样强制执行更细粒度安全性的地方。也许您应该首先考虑防止将不应连接到外部源的内容加载到您的 webview 中。

您可以根据您的用例拦截 WebView 和 allow/disallow 上的请求。

见此方法:

shouldInterceptRequest

Notify the host application of a resource request and allow the application to return the data. If the return value is null, the WebView will continue to load the resource as usual. Otherwise, the return response and data will be used. NOTE: This method is called on a thread other than the UI thread so clients should exercise caution when accessing private data or the view system.