Mdlc
Mdlc

Reputation: 7288

Check if Webpage/Webview contains certain words (android)

I am creating an application, and I would like to give a notification to the user if the a webpage (which displays canceled classes) contains the class you're in.

How can this be done?
Do I have to use a webview and reload every x minutes on the background and check if it contains certain words, and if it does, give a notification? And if this is the way, how do I check for certain words within a webview?

Upvotes: 1

Views: 2947

Answers (3)

anthonycr
anthonycr

Reputation: 4186

This is easily doable in the WebView, no unnecessary code needed. The following code will find the string (you can also set a FindListener to inform you if/when the string is found, I haven't used it but it's there):

String string = "text you want to find";
webview.findAll(string); //works for API levels 3 - 16
webview.findAllAsync(string); //works for API levels 16 and up

As you see, you'll have to use both methods if you want to support API levels below 16. Both have the same listener though so you only need one listener, check the link above.

Upvotes: 2

R Earle Harris
R Earle Harris

Reputation: 983

I have not done this in Android. But I have done it in other environments. You may be able to pull the last page from cache and parse it. Else, use Java to repull each page and parse. Mr. van Raak has a good idea about the service. You may already have one and can add this in. While JSoup is convenient, it is kind of chubby. You could do your own lightweight filter pretty easily.

Upvotes: 1

Edward van Raak
Edward van Raak

Reputation: 4920

If the data is not available by any other means (like an RSS feed or some other kind of URL request) you could scrape the webpage with something like jSoup. You can simply give jSoup the URL of the webpage and it will return the raw HTML file of the webpage. You can travel through the DOM elements and get the exact text you want.

You will also have to create an Android Service that will run even when you are not using the application. Every X minutes this will do the webpage scraping and gives a notification when needed.

Upvotes: 0

Related Questions