5

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

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

bad_coder
  • 11,289
  • 20
  • 44
  • 72
Ammar
  • 1,203
  • 5
  • 27
  • 64
  • 1
    Why do you even *have* a `WebView`? What role is the `WebView` playing? – CommonsWare Mar 16 '13 at 22:13
  • 2
    Does this answer your question? [Android: Using WebView outside an Activity context](https://stackoverflow.com/questions/18865035/android-using-webview-outside-an-activity-context) – Donald Duck Aug 28 '21 at 15:43

3 Answers3

1

Yes, a service runs in the background and should not able able to display any UI.

But you can have an Activity (a UI process) passed its context to a service using PendingIntent.getService(context, GET_ADSERVICE_REQUEST_CODE, ...). Then when the service is ready to display, the lines below should launch browser (or you owner app with appropriate Intent Filter for own WebView) to display the web content.

Intent i = new Intent(Intent.ACTION_VIEW, url);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, i,
        Intent.FLAG_ACTIVITY_NEW_TASK);
Donald Duck
  • 8,409
  • 22
  • 75
  • 99
Kevin T.
  • 13
  • 1
-1

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;
}
323go
  • 14,143
  • 6
  • 33
  • 41
  • 3
    "a WebView can not be used inside a service" -- actually, that's not strictly true, though in this case your solution is what is appropriate, most likely. Though I would recommend `Log.e()` instead of `printStackTrace()`, and Google recommends `HttpUrlConnection` over HttpClient. – CommonsWare Mar 16 '13 at 22:27
  • @CommonsWare, changed my "can" to "should" and replaced the Log.e. I'm aware of Google's preference for HttpUrlConnection, but for clarity's sake, I prefer HttpClient. Old habits. – 323go Mar 16 '13 at 22:36
  • @323go How about keeping the sesstion or cookies or ...? for example,I login to website so if I use your solution,How should I make request for authorized page? – mohsen Jul 30 '21 at 16:34
-1

Yes, you can use a static Webview from another activity of your project. for example: MainActivity.web2.loadUrl ( "" ); in your Service.