I am making an Android app to get the posts from my University using Kotlin. For now I have a button to refresh the page manually and a drop-down menu from which I can choose the period of time for refreshing the page and getting the data. I am using Jsoup to get the data and then I split the data and make it an Array. I want an email to be sent, or a notification which would be better if something changes.
class MainActivity : AppCompatActivity(){
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
lateinit var refresh: Runnable
val handler10 = Handler()
spinnerVrijeme.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(adapterView: AdapterView<*>?, view: View?, position: Int, id: Long) {
if(adapterView?.getItemAtPosition(position).toString()=="10 minutes")
{
Toast.makeText(this@MainActivity, "You chose${adapterView?.getItemAtPosition(position).toString()}",
Toast.LENGTH_LONG).show()
refresh = Runnable { // Do something
getHtmlFromWeb()
handler10.postDelayed(refresh, 1000*60*10)
clearTextView()
}
handler10.post(refresh)
}
button_click.setOnClickListener()
{
// your code to perform when the user clicks on the button
//Toast.makeText(this@MainActivity, "Page Loading...", Toast.LENGTH_SHORT).show()
getHtmlFromWeb()
}
And then I have a function to get the data from the website:
@SuppressLint("SetTextI18n")
public fun getHtmlFromWeb() {
val executor: ExecutorService = Executors.newSingleThreadExecutor()
val handler = Handler(Looper.getMainLooper())
executor.execute(Runnable {
//Background work here
val doc = Jsoup.connect("websiteURL").get()
val smjer = doc.select("div.col-md-6.col-lg-12 > *").text()
val smjer_cleaned = Jsoup.clean(smjer, "", Whitelist.none(), Document.OutputSettings().prettyPrint(false))
val split_all = smjer_cleaned.split(" ").toTypedArray()
var split_all_copy = split_all.copyOf()
handler.post(Runnable {
//UI Thread work here
for(i in 0..split_all.size - 1){
PrikazStranice.append(split_all.elementAt(i) "\n\n")
}
PrikazStranice.movementMethod = ScrollingMovementMethod()
fun WebsiteChanged (){
for (i in 0..split_all.size-1)
{
if(split_all_copy.elementAt(i) != split_all.elementAt(i))
{
//Do something
//Store the new Array<String>
split_all.elementAt(i) = split_all_copy.elementAt(i)
}
}
}
})
})
}
So, for now I can manually press the button and the posts will be shown in TextView, but I would like to automate it so the change will be recognized. I was thinking that I have to call the WebsiteChange() function where I refresh the page every 10 minutes for example, and there, catch the new String Array which I would compare to the original one, and then give the original one the new value. Right now, I don't have an idea how I could make a function WebsiteChange() outside of getHtmlFromWeb() and get the original String Array from there and use it in the right way. I just picked up on Kotlin and Android apps development, so any suggestion is welcome.
CodePudding user response: