可以的WebView是服务内使用? [英] Can WebView be used inside a service?

查看:151
本文介绍了可以的WebView是服务内使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有检查特定网站每隔一分钟,看它是否发现不管我在寻找一个应用程序,然后通知我(播放声音)每当项目被找到。我跟着这个啧啧,使在后台我的应用程序运行,但我注意到它抱怨web视图。

I have an app that checks a specific website every one minute to see if it finds whatever I am looking for, then notifies me (Plays Sound) whenever the item is found. I followed this tut to make my app run in the background, but I noticed it complains about the WebView.

http://marakana.com/forums/android/examples/60.html

如果这是不可能使用一个服务里面的WebView,我有哪些替代品达到同样的目标是什么?

If it's not possible to use a WebView inside a service, what are my alternatives to achieve the same goal?

感谢您!

推荐答案

没有,一个的WebView 不应在服务中使用,它真的没有意义,反正。如果你加载你的的WebView 与刮包含的HTML的意图,你可能也只是运行一个HTTPGET请求时,这样的 -

No, a WebView should not be used inside a service, and it really doesn't make sense to, anyway. If you're loading your WebView with the intention of scraping the html contained in it, you might as well just run an HttpGet request, like this --

public static String readFromUrl( String url ) {
    String result = null;

    HttpClient client = new DefaultHttpClient();

    HttpGet get = new HttpGet( url ); 

    HttpResponse response;
    try {
        response = client.execute( get );
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            InputStream is = entity.getContent();
            BufferedReader reader = new BufferedReader(
                                        new InputStreamReader( is ) );
            StringBuilder sb = new StringBuilder();

            String line = null;
            try {
                while( ( line = reader.readLine() ) != null )
                    sb.append( line + "\n" );
            } catch ( IOException e ) {
                Log.e( "readFromUrl", e.getMessage() );
            } finally {
                try {
                    is.close();
                } catch ( IOException e ) {
                    Log.e( "readFromUrl", e.getMessage() );
                }
            }

            result = sb.toString();
            is.close();
        }


    } catch( Exception e ) {
        Log.e( "readFromUrl", e.getMessage() );
    }

    return result;
}

这篇关于可以的WebView是服务内使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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