Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Some help with looper

  1. #1
    Junior Member
    Join Date
    Aug 2014
    Posts
    29
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Some help with looper

    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:
    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
    	}
    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().
    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


  2. #2
    Forum VIP
    Join Date
    Jun 2011
    Posts
    317
    My Mood
    Bored
    Thanks
    47
    Thanked 89 Times in 74 Posts
    Blog Entries
    4

    Default Re: Some help with looper

    Quote Originally Posted by skarosg3 View Post
    when i realised i had to use Looper(something i havent done before)
    What lead to this realisation? Why do you need a looper?

    Quote Originally Posted by skarosg3 View Post
    I want my application to work on the background (in a way the user doesnt realise)
    This sounds like a job for an asynchtask. They are frequently used for background processes that don't block the UI thread. Looper are for creating a queue of runnables. The problem it solves is very specific and in three years I've only used them once (it was a mistake). Can you explain to me why it's necessary and why an asynch doesn't suffice?
    Computers are fascinating machines, but they're mostly a reflection of the people using them.
    -- Jeff Atwood

  3. The Following 2 Users Say Thank You to ChristopherLowe For This Useful Post:

    GregBrannon (September 26th, 2014), skarosg3 (September 26th, 2014)

  4. #3
    Junior Member
    Join Date
    Aug 2014
    Posts
    29
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Some help with looper

    I didnt think of AsyncTask. Will give it a try.
    At first i run the code as described above, and when executed, i got an error saying that
    java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

    Thats why I start looking at looper.

    I tried to implement it using AsyncTask, but again when it came acros the toast command, i got the same error.

    --- Update ---

    OK, figured it out.
    I had to use the toast command on the onPostExecute() method.
    now it seems to be working.

    Thanks

Similar Threads

  1. Looper
    By Hunter1904 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 28th, 2013, 05:00 AM