Simple GoogleMaps with Threads

Tutorials that use the MapActivity. Many using GPS functionality.

Re: Simple GoogleMaps with Threads

Postby bsodmike » Sat Dec 25, 2010 1:37 pm

Hi plusminus,

I've just been googling various examples for implementing threading on some apps and I've come across a few approaches.

1. http://codinghard.wordpress.com/2009/05 ... messaging/

Purely uses handlers and messaging; whilst debugging I can see that a child thread is created and it persists - it is also a singular thread similar to (3).

2. I tried to put together an example of threading as runnable, http://pastie.org/pastes/1400971

You will see some notes of the issues I ran into. In this example, I noticed that the thread would quit on completion. On each click of the button it'd load a new parallel thread - a pipeline thread isn't maintained.

3. http://mindtherobot.com/blog/159/androi ... -handlers/

This works as advertised, once I ported all the code into a SDK 2.2 project. However, I'm looking for something more elegant such as your solution here (am still testing this, or to be more specific, getting my head around it!).

Questions:

A. is there a particularly best practice type approach here?

B. messaging vs. runnable - thoughts?

C. http://pastie.org/pastes/1400971 << any thoughts on this?

D. I took your approach, yanked out the Geo stuff just to see how the threading works and it's similar in operation to *my* messy approach that I took with runnable in (C) above.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         public void onClick(View v) {
  2.                              
  3.                 Thread pipelineThread = new Thread() {
  4.                         public void run(){
  5.                                 final String TAG = "pipelineThread";
  6.                                 try {
  7.                                         Log.i(TAG, "before sleep");
  8.                                         Thread.sleep(1500); //just to show you that it works
  9.                                         Log.i(TAG, "after sleep");
  10.                                 } catch (Exception e) {
  11.                                         // @todo: Show error message
  12.                                 }
  13.                                 showResults.sendEmptyMessage(0);                                        
  14.                         }
  15.                 };
  16.                 pipelineThread.start();
  17.         }
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


Thanks,
Mike

mike@bsodmike.com / @bsodmike on Twitter
Last edited by bsodmike on Sat Dec 25, 2010 4:00 pm, edited 1 time in total.
bsodmike
Freshman
Freshman
 
Posts: 4
Joined: Sat Dec 25, 2010 1:29 pm

Top

Re: Simple GoogleMaps with Threads

Postby bsodmike » Sat Dec 25, 2010 2:32 pm

Here's the source of my first attempt with your Geo approach, and the same implementing the activity as Runnable.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.threading;
  2.  
  3. import android.app.Activity;
  4. import android.os.Bundle;
  5. import android.os.Handler;
  6. import android.os.Message;
  7. import android.util.Log;
  8. import android.view.View;
  9. import android.view.View.OnClickListener;
  10. import android.widget.Button;
  11. import android.widget.ProgressBar;
  12. import android.widget.TextView;
  13.  
  14. public class ThreadingActivity extends Activity implements OnClickListener, Runnable {
  15.         Thread pipelineThread;
  16.     /** Called when the activity is first created. */
  17.     @Override
  18.     public void onCreate(Bundle savedInstanceState) {
  19.                 super.onCreate(savedInstanceState);
  20.                 setContentView(R.layout.main);
  21.                
  22.                 TextView textOutput = (TextView)findViewById(R.id.textOutput);
  23.                 Button buttonPush = (Button)findViewById(R.id.buttonPush); // Get button from xml
  24.         ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar);        
  25.                
  26.                 buttonPush.setOnClickListener(this);
  27.         }
  28.  
  29.  
  30.     public void onClick(View v) {
  31.                          
  32.         pipelineThread = new Thread(this) {
  33.                 /*
  34.                  * 1. uncomment this and "Runnable" from above
  35.                  * 2. comment lines 68-80
  36.                  */
  37. //              public void run(){
  38. //                      final String TAG = "pipelineThread";
  39. //                      try {
  40. //                              Log.i(TAG, "before sleep (" + this.getName() + ")");
  41. //                              Thread.sleep(1500); //just to show you that it works
  42. //                              Log.i(TAG, "after sleep (" + this.getName() + ")");
  43. //                      } catch (Exception e) {
  44. //                              // @todo: Show error message
  45. //                      }
  46. //                      showResults.sendEmptyMessage(0);                                        
  47. //              }
  48.             };
  49.             pipelineThread.start();
  50.     }
  51.  
  52.  
  53.     private Handler showResults = new Handler() {
  54.         public static final String TAG = "handler_showResults";
  55.        
  56.         @Override
  57.         public void handleMessage(Message msg) {
  58.                 Log.i(TAG, "received message: "+ (Object)msg.obj +  " (" + this.getLooper().getThread().getName() + ")");
  59.        
  60.                 super.handleMessage(msg);
  61.         }
  62.     };
  63.  
  64.  
  65.     /*
  66.      * ThreadingActivity implements Runnable
  67.      */
  68.     @Override
  69.         public void run() {
  70.                 // TODO Auto-generated method stub
  71.         final String TAG = "pipelineThread";
  72.         try {
  73.                 Log.i(TAG, "before sleep (" + pipelineThread.getName() + ")");
  74.                 Thread.sleep(1500); //just to show you that it works
  75.                 Log.i(TAG, "after sleep (" + pipelineThread.getName() + ")");
  76.         } catch (Exception e) {
  77.             // @todo: Show error message
  78.         }
  79.     showResults.sendEmptyMessage(0);                   
  80.         }
  81.                
  82. }
  83.  
  84.  
Parsed in 0.041 seconds, using GeSHi 1.0.8.4


The output of running this as Runnable is different; if I trigger a mass of clicks - only the last thread handles all the responding.
Code: Select all
I/ActivityManager(   66): Displayed activity com.threading/.ThreadingActivity: 5014 ms (total 5014 ms)
I/pipelineThread( 1308): before sleep (Thread-8)
I/pipelineThread( 1308): before sleep (Thread-9)
I/pipelineThread( 1308): before sleep (Thread-10)
I/pipelineThread( 1308): after sleep (Thread-10)
I/handler_showResults( 1308): received message: null (main)
I/pipelineThread( 1308): after sleep (Thread-10)
I/handler_showResults( 1308): received message: null (main)
I/pipelineThread( 1308): after sleep (Thread-10)
I/handler_showResults( 1308): received message: null (main)


Running as per plusminus's preferred way,
Code: Select all
I/ActivityManager(   66): Displayed activity com.threading/.ThreadingActivity: 1680 ms (total 1680 ms)
I/pipelineThread( 1339): before sleep (Thread-8)
I/pipelineThread( 1339): before sleep (Thread-9)
I/pipelineThread( 1339): before sleep (Thread-10)
I/pipelineThread( 1339): after sleep (Thread-8)
I/handler_showResults( 1339): received message: null (main)
I/pipelineThread( 1339): after sleep (Thread-9)
I/handler_showResults( 1339): received message: null (main)
I/pipelineThread( 1339): after sleep (Thread-10)
I/handler_showResults( 1339): received message: null (main)
bsodmike
Freshman
Freshman
 
Posts: 4
Joined: Sat Dec 25, 2010 1:29 pm

Top
Previous

Return to Map Tutorials

Who is online

Users browsing this forum: No registered users and 3 guests