My app has a WebView and I want to configure it to use a proxy. Apparently Android doesn't have an API that I can use to achieve this but I found some a couple of articles on StackOverflow showing how to do it via reflection:
WebView android proxy
Android WebView set proxy programmatically on Android-L
Unfortunately the methods in the first article only work up to KitKat 4.4 and the Android L/5.0 way of doing it requires setting the application-wide proxy settings (via System.setProperty("http.proxyHost", ...) and System.setProperty("http.proxyPort", ...) which affects more than just the WebView. For example, the Apache HTTP client seems to pick up these settings too.
Is there a way to set proxy settings just for WebViews without affecting other components of the app?
In API>21 lollipop, it does not allow setting proxy settings in webviews. The methods have been removed.
So the only way now is to set system wide proxy as you mentioned, then clear the proxy on onPause and onStop methods of your activity.
The clear can be done by:
System.clearProperty("http.proxyHost");
System.clearProperty("http.proxyPort");
Hope you find this helpful.
Related
I have installed on my android device (V 5.1.1) two package for Android System WebView:
com.android.webview
com.google.android.webview
I'm trying to use the second package, following different ways, deactivating or uninstalling (impossible) first, and now I want to force the second when I load my WebView directly by code.
Do you know any solutions?
PS: I know and in other cases, I'm used CustomTabs, but in this case, I can't Chrome or Chromium on my device.
Seems there is no way to change default WebView client without root on devices. Please check this answer.
I have recently started automation testing in android and was using UiAutomator tool for inspecting UI elements. Surprisingly, I came across two different responses for Webview elements for the same screen and same device [Genymotion MotoX 4.4.4]. Attached are the screenshots for the same. We all know that appium has an issue with inspecting webview elements who do not carry "setWebContentsDebuggingEnabled" to true. Is this a bug in UiAutomator?
We all know that appium has an issue with inspecting webview elements
who do not carry "setWebContentsDebuggingEnabled" to true. Is this a
bug in UiAutomator?
NO, its not a bug. Its an IMPLEMENTATION as is. Unless the setWebContentsDebuggingEnabled is set to true, you cannot debug or access any elements inside a webview.
To quote from developers.android.com
void setWebContentsDebuggingEnabled (boolean enabled)
Enables
debugging of web contents (HTML / CSS / JavaScript) loaded into any
WebViews of this application. This flag can be enabled in order to
facilitate debugging of web layouts and JavaScript code running inside
WebViews. Please refer to WebView documentation for the debugging
guide. The default is false.
You might be interested into these as well :
Migrating to WebView in Android 4.4
I'm having some trouble getting TalkBack to work with a web view (testing with a Nexus 5 on Android 4.4.2). I read that TalkBack support was added to web views around the release of Android JellyBean by checking a preference titled "Enhance Web Accessibility." I can't for the life of me find this preference in the system settings.
Focusing on the web view in our application simply reads "WebView" and provides no other options. I've made sure and called getSettings().setJavaScriptEnabled(true); on the web view.
Was TalkBack support for web views removed in KitKat? If not, what am I missing here?
On 4.4, you need to have TalkBack/Explore by Touch enabled on the first run of your application. If you enabled it after that, it never seems to work. I figured this out by trial and error. I suspect scripts are being injected into your app/WebView on first launch to support it.
As an alternative, you could manually inject the ChromeVox scripts into your WebView and enable accessibility that way. Check out how the PhoneGap plugin does it - https://github.com/phonegap/phonegap-mobile-accessibility
It seems to me that there is a lot of confusing resources regarding the proxy topic on Android.
First of all, it seems that all the methods of the Proxy class are declared deprecated and it's suggested to:
"Use standard java vm proxy values to find the host, port and
exclusion list. This call ignores the exclusion list."
The official java vm proxy values can be accessed in the following way:
System.getProperty("http.proxyHost")
System.getProperty("http.proxyPort")
System.getProperty("http.nonProxyHosts")
This could seem confirmed by the documentation of the ProxySelector class.
But trying on a real device or an emulator, these values seems to me always empty. After looking to the Android source code of the hidden ProxySelector activity, it seems that the proxy is saved into the secure settings of the system in the following way:
Settings.Secure.putString(res, Settings.Secure.HTTP_PROXY, hostname);
And only an application signed by the OS provider can write to the secure settings. Developers can access these settings only in read mode in the following way:
Settings.Secure.getString(getApplicationContext().getContentResolver(),Settings.Secure.HTTP_PROXY);
Someone can clarify if this is the correct reading of how can be access the proxy settings into Android? (At least it seems to work). If this is the correct intepretation, why the documentation is so full of errors?
For getting proxy values, accessing the System properties as you have done should work; it should not be necessary to access secure settings. If you cannot use the System properties to read proxy settings that were made through the normal device UI, then there is a problem. Proxies are per network type, so the APN and WiFi will have separate proxy settings.
I don't know if it's the "right" way to access the proxy settings but it's the right and only way you should access the system "secure settings".
Maybe this is also interesting, looks like it makes things easier, especially if there are Wifi proxys (does Android support something like this?). At least it looks like great abstraction for the various android versions.
I am just wondering how to enable/disable 3G/2G using the Android SDK and not just intenting to the 3G settings page. Thanks. Also is there a way to do the same thing, but with GPS. Thanks!
There is no exported to the SDK functionality to switch between 2G and 3G. For a given device you could probably figure out the private functionality, but it wouldn't work unless the app was signed with the system key.
You can disable the radios though, by turning on airplane mode.
And you might be able to make a shortcut to open the appropriate settings activity directly, instead of going through a few levels of menus to get there.
If you make your own build, you can presumably add the capability you really want, but that's likely not useful to anyone but yourself.
Edit: further detail - to understand how it works, look at the settings menu code (default version, a given device will differ):
http://android.git.kernel.org/?p=platform/packages/apps/Phone.git;a=blob;f=src/com/android/phone/Settings.java;hb=HEAD
Edit: AOSP is no longer served from kernel.org. A browsable mirror of
the current phone repository is at
https://github.com/android/platform_packages_apps_phone however due to
evolution of code organization there is no longer a Settings.java
there. One might either use git to reconstruct the version above from
this repository, or else try to figure out where the code has migrated
to in current releases.
You would need to find out the implementation specific set of NT_MODE_ constants you wish to toggle between. And you need to be able to write to secure settings, which requires being signed with the system key. Unless it's a custom build, you probably don't have access to that.
You cannot enable or disable any of these from an SDK application.