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:
Using java Syntax Highlighting
- import com.google.android.maps.Point;
- import com.google.googlenav.map.MapPoint;
- import android.app.Activity;
- import android.content.Context;
- import android.os.Bundle;
- import android.os.Handler;
- import android.os.Message;
- import android.util.Log;
- public class MyData extends Activity {
- private Thread thread = null;
- private MyLocation myloc = null;
- private MapPoint mp;
- private GraphicsView graphicsview = null;
- private Context context;
- protected static final int GUIUPDATEIDENTIFIER = 0x101;
- public int counter=0;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle icicle) {
- context = this;
- super.onCreate(icicle);
- graphicsview = new GraphicsView(this);
- setContentView(graphicsview);
- init();
- }
- private void init(){
- Runnable runnable = new thread();
- thread = new Thread(runnable);
- thread.start();
- }
- public void onDestroy(){
- this.thread.interrupt();
- finish();
- }
- public void finalize(){
- this.thread.interrupt();
- finish();
- }
- Handler myViewUpdateHandler = new Handler(){
- // @Override
- public void handleMessage(Message msg) {
- switch (msg.what) {
- case MyData.GUIUPDATEIDENTIFIER:
- //Do the update / refresh stuff
- // Get the GPS information and set them to myactualposition
- myloc = new MyLocation(context);
- mp = myloc.getmyLocation();
- Log.d("mydata", "I'm in the Handler doing submission "+counter);
- Log.d("mydata", "GPS Object: "+mp.toString());
- graphicsview.setBackground(123455);
- graphicsview.invalidate();
- counter++;
- break;
- }
- super.handleMessage(msg);
- }
- };
- class thread implements Runnable {
- // implements Runnable ....could be done if you want to have more threads access on a object
- public boolean testat = false;
- /*
- * Starts the thread and makes a Message so that a Handler can get it.
- * Done because only the initialiesed thread could make a update so a Handler is needed
- * see also picture at instantmessaging_display_incoming_messages_without_action-t162.html
- * (non-Javadoc)
- * @see java.lang.Runnable#run()
- */
- // @Override
- public void run() {
- while(!Thread.currentThread().isInterrupted()) {
- Message m = new Message();
- m.what = MyData.GUIUPDATEIDENTIFIER;
- MyData.this.myViewUpdateHandler.sendMessage(m);
- try {
- Thread.sleep(10000);
- }
- catch (InterruptedException e) {
- Thread.currentThread().interrupt();
- }
- }
- }
- }
- public boolean onKeyDown(int keyCode, KeyEvent event) {
- switch (keyCode) {
- case KeyEvent.KEYCODE_BACK:
- thread.interrupt();
- thgroup.interrupt();
- break;
- case KeyEvent.KEYCODE_HOME:
- thread.interrupt();
- thgroup.interrupt();
- this.threadrunning = !this.threadrunning;
- Log.d("mydata", "Key home pressed");
- break;
- case KeyEvent.KEYCODE_DPAD_CENTER :
- thread.interrupt();
- thgroup.interrupt();
- this.threadrunning = !this.threadrunning;
- Log.d("mydata", "Key DPAD CENTER pressed");
- break;
- case KeyEvent.KEYCODE_ENDCALL :
- thread.interrupt();
- thgroup.interrupt();
- Log.d("mydata", "Key endcall pressed");
- break;
- case KeyEvent.ACTION_DOWN :
- Log.d("mydata", "Key down pressed"+keyCode);
- break;
- default:
- break;
- }
- return super.onKeyDown(keyCode, event);
- }
- }
Parsed in 0.046 seconds, using GeSHi 1.0.8.4


