窗口小部件,从几个HT​​ML页面检索数据 [英] Widget that retrieve data from several html pages

查看:129
本文介绍了窗口小部件,从几个HT​​ML页面检索数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我检索HTML一些数据后几个步骤(登录,点击链接,运行JavaScript等)使用的WebView,就像这里描述:的如何从网页视图HTML内容?把更多的行动级联。
我不使用线程,主要是当一个动作(登录为例)完成,这就是所谓的 onPageFinished 并这场大火另一个 webview.loadUrl 等另一个<强> onPageFinished 直到所有的动作将被执行。

我想创建一个小部件,显示此结果,火这条产业链中的​​ webview.loadUrl 当小部件的刷新按钮pssed $ P $或12AM每一天。所有的链条完成后才能,结果可以在小部件被显示。

我读过一些教程,我看到有很不同的方法,TimerTask的,服务,广播接收器等。但我不明白这适合我的项目。
我不知道我的解释是明确的,但在小部件的的onUpdate,我开始768,16在 webview.loadUrl 序列,但我不能,直到返回任何最后的 onPageFinished 被调用。

这是我的意思连锁行动,urlChain是一个静态的字符串的LinkedList使用URL的序列(和其他东西)。

  @燮pressLint(SetJavaScriptEnabled)
私人无效callChain()
{
    / * WebViewClient必须设置前调用使用loadURL! * /
    browser.setWebViewClient(新WebViewClient(){
        @覆盖
        公共无效onPageFinished(的WebView视图,字符串URL)
        {
            / *这个调用JavaScript插入刚刚完成加载的页面。 * /
            如果(!urlChain.isEmpty())
            {
                callChain();
            }
        }    });    / *加载网页* /
    如果(!urlChain.isEmpty())
    {
        串CURRENTURL;
        CURRENTURL = urlChain.pop();
        browser.loadUrl(CURRENTURL);
    }
}


解决方案

这可能是一个服务或其它一些独立的结构进行后台工作是一个好主意, <一href=\"http://stackoverflow.com/questions/3455123/programmatically-update-widget-from-activity\">fireing更新到部件或<一个href=\"http://developer.android.com/reference/android/appwidget/AppWidgetManager.html#updateAppWidget%28int%5B%5D,%20android.widget.RemoteViews%29\"相对=nofollow>上的所有部件执行直接更新之后的,然后使新的数据显示获得


不知道你的确切项目什么,最彻底的方法可能会是一个 SyncAdapter ,peforming在后台同步和更新的ContentProvider 更新窗口小部件时,可后来就习惯。

不过,这可能是矫枉过正(你需要在系统中注册一个帐户同步,等等),根据您的实际需求。你可能也只是有一个服务小部件可以要求更新,然后使用相同的技术,一旦完成加载触发更新。

哦,如果你一定要准确12时许,看看 AlarmManager

I retrieve some data from html after several step (login, click on link, run javascript, etc.) using webview, like described here: how to get html content from a webview? putting more actions in cascade. I don't use thread, mainly when an action (login for example) is done, it's called onPageFinished and this fire another webview.loadUrl and so on to another onPageFinished till all the actions are executed.

I want to create a widget that show this result, that fire this chain of webview.loadUrl when the widget refresh button is pressed or every day at 12AM. Only after that all the chain is completed, the result can be showed in the widget.

I've read some tutorials, and I saw that there're different approach, TimerTask, Service, Broadcast receiver, ect., but I didn't understand which fit to my project. I don't know if my explaination was clear, but in the onUpdate of the widget, I shoud start the webview.loadUrl sequence, but I can't return anything until the last onPageFinished is called.

This is what I mean for chain action, urlChain is a static string LinkedList with the sequence of URLs (and other stuff).

@SuppressLint("SetJavaScriptEnabled")
private void callChain()
{
    /* WebViewClient must be set BEFORE calling loadUrl! */
    browser.setWebViewClient(new WebViewClient() {
        @Override
        public void onPageFinished(WebView view, String url)
        {
            /* This call inject JavaScript into the page which just finished loading. */
            if (!urlChain.isEmpty())
            {
                callChain();
            }
        }

    });

    /* load a web page */
    if (!urlChain.isEmpty())
    {
        String currentUrl;
        currentUrl = urlChain.pop();
        browser.loadUrl(currentUrl);
    }
}

解决方案

It is probably a good idea to perform the background work on a Service or some other independent structure, fireing an update to the widget or performing an direct update on all widgets afterwards, which then causes the fresh data to get displayed.


Without knowing anything about your exact project, the cleanest way would probably be a SyncAdapter, peforming the synchronization in the background and updating a ContentProvider which can then get used when updating the widget.

However this may be overkill (you need to register a sync account in the system, etc), depending on your actual needs. You could as well just have a Service the widget may request an update on, which then fires an update using the same technique once it finished loading.

Oh, and if you're bound to exactly 12 AM, have a look at AlarmManager.

这篇关于窗口小部件,从几个HT​​ML页面检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆