简体   繁体   中英

Android: preventing only a single activity from accessing internet

Android apps declare what permissions they need to work, so the user is informed.

Let's say my Android app declares internet permission but it just uses internet access in a single activity and wants to prevent another activity from accessing the internet instead.

Let's say the activity that is not intended to access the internet has a WebView that performs some javascript processing on some HTML files provided by the user: any hacking has to be prevented so no internet access can be granted to this activity, or the WebView itself.

Is it possible?

The android framework level is not the spot to be enforcing finer grained security like this. Perhaps you should look into preventing content being loaded at all into your webview in the first place that should not be connecting to outside sources.

You can intercept requests on a WebView and allow/disallow based on your use case.

See this method:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM