Problem stopping a Thread with Home Button or ENDCALL

Put your problem here if it does not fit any of the other categories.

Problem stopping a Thread with Home Button or ENDCALL

Postby cbraun75 » Tue Jun 03, 2008 11:38 am

Hi!

Just have a Problem with stopping a thread by finishing a programm through
- just go back to mainmenu or go back with the back button.

It is replying always on in the Log modul the data.

My programm catches the Location data from the GPS modul and should send it to a webservice. But when the home button or the back button is pressed the threads are always going on ... and telling the thread that there is an interrupt for him doesn't make anything.

I tried it like the code at this
TOPIC

Has anybody an idea why it's always running?

I tried to implement another method in the thread class called stop this method set an boolean to true and the while loop is then set to this boolean variable but from outside i can't set the variable to a diferent state because he can't get the function or variable in the inner class ....

My code is:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. import com.google.android.maps.Point;
  2. import com.google.googlenav.map.MapPoint;
  3. import android.app.Activity;
  4. import android.content.Context;
  5. import android.os.Bundle;
  6. import android.os.Handler;
  7. import android.os.Message;
  8. import android.util.Log;
  9.  
  10. public class MyData extends Activity {
  11.    
  12.         private Thread thread = null;
  13.         private MyLocation myloc = null;
  14.         private MapPoint mp;
  15.         private GraphicsView graphicsview = null;
  16.         private Context context;
  17.         protected static final int GUIUPDATEIDENTIFIER = 0x101;
  18.     public int counter=0;
  19.    
  20.         /** Called when the activity is first created. */
  21.     @Override
  22.     public void onCreate(Bundle icicle) {
  23.         context = this;
  24.         super.onCreate(icicle);
  25.         graphicsview = new GraphicsView(this);
  26.         setContentView(graphicsview);
  27.         init();        
  28.     }
  29.    
  30.     private void init(){
  31.         Runnable runnable = new thread();
  32.         thread = new Thread(runnable);  
  33.         thread.start();        
  34.        
  35.     }  
  36.    
  37.     public void onDestroy(){
  38.         this.thread.interrupt();
  39.         finish();
  40.     }
  41.    
  42.     public void finalize(){
  43.         this.thread.interrupt();
  44.         finish();
  45.     }
  46.    
  47.     Handler myViewUpdateHandler = new Handler(){
  48.         // @Override
  49.         public void handleMessage(Message msg) {
  50.              switch (msg.what) {
  51.                   case MyData.GUIUPDATEIDENTIFIER:
  52.                           //Do the update / refresh stuff
  53.                        
  54.                           // Get the GPS information and set them to myactualposition
  55.                           myloc = new MyLocation(context);
  56.                           mp = myloc.getmyLocation();  
  57.                          
  58.                           Log.d("mydata", "I'm in the Handler doing submission "+counter);
  59.                           Log.d("mydata", "GPS Object:  "+mp.toString());
  60.                           graphicsview.setBackground(123455);
  61.                           graphicsview.invalidate();
  62.                           counter++;
  63.                   break;
  64.              }
  65.              super.handleMessage(msg);
  66.         }
  67.    };
  68.    
  69.    class thread implements Runnable {
  70.            // implements Runnable ....could be done if you want to have more threads access on a object
  71.            public boolean testat = false;
  72.        
  73.                 /*
  74.                  * Starts the thread and makes a Message so that a Handler can get it.
  75.                  * Done because only the initialiesed thread could make a update so a Handler is needed
  76.                  * see also picture at instantmessaging_display_incoming_messages_without_action-t162.html
  77.                  * (non-Javadoc)
  78.                  * @see java.lang.Runnable#run()
  79.                  */
  80.                 //       @Override     
  81.        public void run() {                             
  82.             while(!Thread.currentThread().isInterrupted()) {
  83.                   Message m = new Message();
  84.                  m.what = MyData.GUIUPDATEIDENTIFIER;
  85.                  MyData.this.myViewUpdateHandler.sendMessage(m);
  86.                  
  87.                  try {
  88.                       Thread.sleep(10000);
  89.                  }
  90.                  catch (InterruptedException e) {
  91.                       Thread.currentThread().interrupt();
  92.                  }
  93.             }
  94.        }
  95.  
  96.    }
  97.         public boolean onKeyDown(int keyCode, KeyEvent event) {
  98.                 switch (keyCode) {
  99.                 case KeyEvent.KEYCODE_BACK:
  100.                         thread.interrupt();
  101.                         thgroup.interrupt();
  102.                         break;
  103.                 case KeyEvent.KEYCODE_HOME:
  104.                         thread.interrupt();
  105.                         thgroup.interrupt();
  106.                         this.threadrunning = !this.threadrunning;
  107.                         Log.d("mydata", "Key home pressed");
  108.                         break;
  109.                 case KeyEvent.KEYCODE_DPAD_CENTER :
  110.                         thread.interrupt();
  111.                         thgroup.interrupt();
  112.                         this.threadrunning = !this.threadrunning;
  113.                         Log.d("mydata", "Key DPAD CENTER pressed");
  114.                         break;
  115.                 case KeyEvent.KEYCODE_ENDCALL :
  116.                         thread.interrupt();
  117.                         thgroup.interrupt();
  118.                         Log.d("mydata", "Key endcall pressed");
  119.                         break;
  120.                 case KeyEvent.ACTION_DOWN :
  121.                         Log.d("mydata", "Key down pressed"+keyCode);
  122.                         break;
  123.                 default:
  124.                        
  125.                         break;
  126.                 }
  127.                 return super.onKeyDown(keyCode, event);
  128.         }
  129. }
Parsed in 0.046 seconds, using GeSHi 1.0.8.4
Last edited by cbraun75 on Tue Jun 03, 2008 2:24 pm, edited 2 times in total.
cbraun75
Junior Developer
Junior Developer
 
Posts: 23
Joined: Mon Jan 28, 2008 9:12 pm
Location: Munich

Top

Postby cbraun75 » Tue Jun 03, 2008 1:23 pm

So a new idea was to make a thread group and from this group stop all threads in this group.

At the back button it works now but not for the home button here the threads are always running allthough I have pressed the home button and have regognized it with the key events ....

Has anybody an idea to stop the threads whatever is pressed?

Code for the threadgroup:
...
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         thgroup = new ThreadGroup("testat");
  2.  
  3.         Runnable runnable = new thread();
  4.  
  5.         thread = new Thread(thgroup,runnable,"UIThread");  
  6.  
  7.         thread.start();
Parsed in 0.031 seconds, using GeSHi 1.0.8.4

...

tried it with following keycodes but ONLY BACK works ...

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         public boolean onKeyDown(int keyCode, KeyEvent event) {
  2.  
  3.                 switch (keyCode) {
  4.  
  5.                 case KeyEvent.KEYCODE_BACK:
  6.  
  7.                         this.thread.interrupt();
  8.  
  9.                         thgroup.stop();
  10.  
  11.                         break;
  12.  
  13.                 case KeyEvent.KEYCODE_HOME:
  14.  
  15.                         this.thread.interrupt();
  16.  
  17.                         thgroup.stop();
  18.  
  19.                         break;
  20.  
  21.                 case KeyEvent.KEYCODE_CAP :
  22.  
  23.                         this.thread.interrupt();
  24.  
  25.                         thgroup.stop();
  26.  
  27.                         break;
  28.  
  29.                 case KeyEvent.KEYCODE_ENDCALL :
  30.  
  31.                         this.thread.interrupt();
  32.  
  33.                         thgroup.stop();
  34.  
  35.                         break;
  36.  
  37.                 case KeyEvent.ACTION_DOWN :
  38.  
  39.                         this.thread.interrupt();
  40.  
  41.                         thgroup.stop();
  42.  
  43.                         break;
  44.  
  45.                 }
  46.  
  47.                 return super.onKeyDown(keyCode, event);
  48.  
  49.         }
Parsed in 0.035 seconds, using GeSHi 1.0.8.4
cbraun75
Junior Developer
Junior Developer
 
Posts: 23
Joined: Mon Jan 28, 2008 9:12 pm
Location: Munich

Postby cbraun75 » Tue Jun 03, 2008 2:36 pm

Just have done it - yipehea!

Just interrupted the thread by onPause so it doesn't matter which button to exit is pressed.

Code looks like this ....
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. import com.google.android.maps.Point;
  2.  
  3. import com.google.googlenav.map.MapPoint;
  4.  
  5. import android.app.Activity;
  6.  
  7. import android.content.Context;
  8.  
  9. import android.os.Bundle;
  10.  
  11. import android.os.Handler;
  12.  
  13. import android.os.Message;
  14.  
  15. import android.util.Log;
  16.  
  17. import android.view.KeyEvent;
  18.  
  19.  
  20.  
  21. public class MyData extends Activity {
  22.  
  23.    
  24.  
  25.         private volatile Thread thread = null;
  26.  
  27.         private MyLocation myloc = null;
  28.  
  29.         private MapPoint mp;
  30.  
  31.         private GraphicsView graphicsview = null;
  32.  
  33.         private Context context;
  34.  
  35.         protected static final int GUIUPDATEIDENTIFIER = 0x101;
  36.  
  37.     public int counter=0;
  38.  
  39.     private ThreadGroup thgroup = null;
  40.  
  41.  
  42.  
  43.    
  44.  
  45.         /** Called when the activity is first created. */
  46.  
  47.     @Override
  48.  
  49.     public void onCreate(Bundle icicle) {
  50.  
  51.         context = this;
  52.  
  53.         super.onCreate(icicle);
  54.  
  55.         graphicsview = new GraphicsView(this);
  56.  
  57.         setContentView(graphicsview);
  58.  
  59.         init();        
  60.  
  61.     }
  62.  
  63.    
  64.  
  65.     private void init(){
  66.  
  67.         myloc = new MyLocation(context);
  68.  
  69.         thgroup = new ThreadGroup("testat");
  70.  
  71.         Runnable runnable = new thread();
  72.  
  73.         thread = new Thread(thgroup,runnable,"UIThread");  
  74.  
  75.         thread.start();
  76.  
  77.        
  78.  
  79.     }
  80.  
  81.    
  82.  
  83.     @Override
  84.  
  85.     public void onPause(){
  86.  
  87.                 thread.interrupt();
  88.  
  89.                 super.onPause();
  90.  
  91.     }
  92.  
  93.    
  94.  
  95.     @Override
  96.  
  97.     public void onResume(){
  98.  
  99.         super.onResume();
  100.  
  101.     }
  102.  
  103.    
  104.  
  105.     Handler myViewUpdateHandler = new Handler(){
  106.  
  107.         // @Override
  108.  
  109.         public void handleMessage(Message msg) {
  110.  
  111.              switch (msg.what) {
  112.  
  113.                   case MyData.GUIUPDATEIDENTIFIER:
  114.  
  115.  
  116.  
  117.                                   //Do the update / refresh stuff
  118.  
  119.                                        
  120.  
  121.                                   // Get the GPS information and set them to myactualposition
  122.  
  123.                                   mp = myloc.getmyLocation();  
  124.  
  125.                                  
  126.  
  127.                                   Log.d("mydata", "I'm in the Handler doing submission "+counter);
  128.  
  129.                                   Log.d("mydata", "GPS Object:  "+mp.toString());
  130.  
  131.                                   //Log.d("mydata","Threadgroup data :"+thgroup.toString());
  132.  
  133.                                   graphicsview.setBackgroundColor(123455);
  134.  
  135.                                   graphicsview.invalidate();
  136.  
  137.                                   counter++;
  138.  
  139.  
  140.  
  141.                   break;
  142.  
  143.              }
  144.  
  145.              super.handleMessage(msg);
  146.  
  147.         }
  148.  
  149.    };
  150.  
  151.    
  152.  
  153.    class thread implements Runnable {
  154.  
  155.        
  156.  
  157.                 /*
  158.  
  159.                  * Starts the thread and makes a Message so that a Handler can get it.
  160.  
  161.                  * Done because only the initialiesed thread could make a update so a Handler is needed
  162.  
  163.                  * see also picture at http://www.anddev.org/instantmessaging_ ... -t162.html
  164.  
  165.                  * (non-Javadoc)
  166.  
  167.                  * @see java.lang.Runnable#run()
  168.  
  169.                  */
  170.  
  171.                 //       @Override     
  172.  
  173.  
  174.  
  175.        public void run() {    
  176.  
  177.            Log.d("mydata", "Thread Status davor:  "+Thread.currentThread().isInterrupted());
  178.  
  179.             while(!Thread.currentThread().isInterrupted()) {
  180.  
  181.                         Log.d("mydata", "Thread Status im while:  "+Thread.currentThread().isInterrupted());
  182.  
  183.                         Message m = new Message();
  184.  
  185.                 m.what = MyData.GUIUPDATEIDENTIFIER;
  186.  
  187.                 MyData.this.myViewUpdateHandler.sendMessage(m);
  188.  
  189.                  
  190.  
  191.                 try {
  192.  
  193.                       Thread.sleep(10000);
  194.  
  195.                 }
  196.  
  197.                 catch (InterruptedException e) {
  198.  
  199.                       Thread.currentThread().interrupt();
  200.  
  201.                 }
  202.  
  203.             }  
  204.  
  205.            Log.d("mydata", "Thread Status danach:  "+Thread.currentThread().isInterrupted());
  206.  
  207.        }
  208.  
  209.  
  210.  
  211.    }
  212.  
  213.    
  214.  
  215.         public boolean onKeyDown(int keyCode, KeyEvent event) {
  216.  
  217.                 switch (keyCode) {
  218.  
  219.                 case KeyEvent.KEYCODE_BACK:
  220.  
  221.                         thread.interrupt();
  222.  
  223.                         thgroup.interrupt();
  224.  
  225.                         break;
  226.  
  227.                 case KeyEvent.KEYCODE_HOME:
  228.  
  229.                         thread.interrupt();
  230.  
  231.                         thgroup.interrupt();
  232.  
  233.  
  234.  
  235.                         Log.d("mydata", "Key home pressed");
  236.  
  237.                         break;
  238.  
  239.                 case KeyEvent.KEYCODE_DPAD_CENTER :
  240.  
  241.                         thread.interrupt();
  242.  
  243.                         thgroup.interrupt();
  244.  
  245.  
  246.  
  247.                         Log.d("mydata", "Key DPAD CENTER pressed");
  248.  
  249.                         break;
  250.  
  251.                 case KeyEvent.KEYCODE_ENDCALL :
  252.  
  253.                         thread.interrupt();
  254.  
  255.                         thgroup.interrupt();
  256.  
  257.                         Log.d("mydata", "Key endcall pressed");
  258.  
  259.                         break;
  260.  
  261.                 case KeyEvent.ACTION_DOWN :
  262.  
  263.                         Log.d("mydata", "Key down pressed"+keyCode);
  264.  
  265.                         break;
  266.  
  267.                 default:
  268.  
  269.                        
  270.  
  271.                         break;
  272.  
  273.                 }
  274.  
  275.                 return super.onKeyDown(keyCode, event);
  276.  
  277.         }
  278.  
  279. }
Parsed in 0.053 seconds, using GeSHi 1.0.8.4
cbraun75
Junior Developer
Junior Developer
 
Posts: 23
Joined: Mon Jan 28, 2008 9:12 pm
Location: Munich

Re: Problem stopping a Thread with Home Button or ENDCALL

Postby Profete162 » Thu Oct 14, 2010 2:17 pm

Hello,

I just had a look to this helpfull topic but my problem is the following:

Code: Select all
@Override
   public boolean onKeyDown(int keyCode, KeyEvent event) {
      if ((keyCode == KeyEvent.KEYCODE_BACK)) {
         Log.d(TAG,"back button pressed");
         if(thread!=null){
            thread.interrupt();
            thread.stop();
            thgroup.interrupt();
         }
         else
            finish();
         thread=null;
      }
      return super.onKeyDown(keyCode, event);
   }

and I can see that Log.d(TAG,"back button pressed"); is never called! So impossible to stop the thread.

Thankl a lot for any help.
Profete162
Experienced Developer
Experienced Developer
 
Posts: 67
Joined: Fri Nov 27, 2009 6:07 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 14 guests