Hi all,
I am creating an Android App and i got stack when i realised i had to use Looper(something i havent done before)
I want my application to work on the background (in a way the user doesnt realise) and when the time is right (under some conditions) to pop up a message, a toast for example.
so i have this:
This gave me an error about looper. So after some reasearch i realised that i need to use that, but i have no idea how. With practice i learned that i need a Looper.prepare(), Looper.Loop() and Looper.myLooper().quitSafely().protected void onCreate(Bundle savedInstanceState) { httpclient = new DefaultHttpClient(); httppost = new HttpPost(addrs); super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); try { //Do my stuff time=new getTime().execute().get(); DeleteOld(readTickets()); //start a new thread to communicate with the server new Thread(new Runnable() { public void run() { try {Looper.prepare(); while(true){ long sleepTime=100000;//millisecond (1000ms=1second) Thread.sleep(sleepTime); refresh(); }//while } catch (InterruptedException e){ //ToDo } } }).start(); } catch (InterruptedException | ExecutionException e) { // TODO Auto-generated catch block e.printStackTrace(); } }//onCreate public void refresh(){ try { //Read Data from file CharSequence[] data=readTickets(); //for all lines from the file for (int i=0;i<data.length;i++){ if(condition) { Toast.makeText(getApplicationContext(), "comming", Toast.LENGTH_LONG).show(); } else{ Toast.makeText(getApplicationContext(), "else", Toast.LENGTH_LONG).show(); } }//for } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } // connect to the server }
But i dont know how to use them properly. I have tried some things, but i cant get it right, at the best, i managed to run the refresh function once, getting the pop up message, but then the programm crashed.
Could someone point me to the right direction?
Thanks, ilias