在后台打开WebView
这是我第一次问问题,所以如果有什么问题请告诉我,我会尽快修复。
我们有一位客户希望我们使用本机 Android 应用程序登录他们的服务器,但没有为我们提供自定义方法来执行此操作。他们希望我们使用他们必须登录的当前网站,并在进行身份验证后,在浏览器中检索包含我们所需数据的 XML。之后,在本机应用程序中使用数据。所有这一切都是在用户不知道/看到正在使用浏览器的情况下进行的。恕我直言,一团糟。
当然,我过去从未尝试过这种方法,我的第一次测试让我觉得这是不可能(或极其困难)实现的。每当我尝试在隐藏的 WebView 中加载 URL 时,默认浏览器就会弹出,显示该网站。
我的主要问题是,是否可以加载 webview 并在后台使用它(调用 javascript 等...)?
谢谢。
This is the first time I ask something, so if there is something wrong just tell me and I´ll try to fix it ASAP.
We have a customer that wants us to login in their servers with a native Android app, but without giving us a custom way to do this. They want us to use the current website they have to log and, after authentication takes place, retrieve within the browser a XML which contains the data we need. After that, use the data in the native app. All of this with the user not knowing/seeing that a browser is being used. A total mess IMHO.
Of course, I have never tried this approach in the past and my first tests make me feel like this is impossible (or extremely difficult) to achieve. Whenever I try to load the URL in a hidden WebView the default browser pops up showing the website.
My main question is, is it possible to load a webview and work with it (invoke javascript, etc...) in the background?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用属性 android:visibility="gone" 将 WebView 设置为默认隐藏,在运行时与其交互,然后当您加载数据后需要将其显示给用户时,只需调用 setVisibility(View.VISIBLE)
希望这有帮助!
You could set the WebView to hidden by default with the attribute android:visibility="gone", interact with it at runtime then when you need to show it to the user after you've loaded data, just call setVisibility(View.VISIBLE)
Hope this helps!
Ofc,您必须使用线程:
记住,这非常重要,您不能从线程绘制到主活动。唯一可以在屏幕上绘图的人是主要活动。您可以使用两种方法进行绘制:
一种是使用方法 _mActivity.runOnUiThread(new Runnable() {_ 就像我放置的示例一样。
第二,使用处理程序将消息从线程发送到主活动,其中包含您想要绘制的信息。
*主要活动是当时屏幕上的活动,而不是应用程序的第一个活动
Ofc, you must to use a Thread :
Remember, and thats is VERY important, you CANT draw from thread to the main activity. The only who can draw in the screen is the main activity. You can draw with 2 methods:
One , with the method _mActivity.runOnUiThread(new Runnable() {_ like the example i put.
Two, use a Handler to send messages from thread to main activity with the information that you want to draw.
*Main activity is the activity that its in the screen in that moment, not the first activity of the app