In addition to the posts at http://groups.google.com/group/android-developers/browse_thread/thread/47a310494882eb5a/622000c1d630690f I'm posting a new topic as the other one seems to be closed for further discussion.
OK so I'm receiving traffic messages via UDP and processing them in a new thread that is started in a service. I want to stop the thread when the home or back buttons are pressed as I assume that the user is then definitely closing the application (there is only one Activity). With the current code the thread just doesn't stop - it looks to me as if the service's onDestory() method is never called: ********************** ReceiveThread (irrelevant code is omitted in each class) public class ReceiveThread extends Thread { private volatile boolean isAlive; private ClientService clientService; public ReceiveThread(ClientService service) { clientService = service; isAlive = true; } public void run() { InetAddress RTIserver; try { RTIserver = InetAddress.getByName(host); DatagramSocket socket = new DatagramSocket(); byte[] buf = new byte[256]; DatagramPacket packet = new DatagramPacket(buf, buf.length, RTIserver, port); BinaryMessage binaryMsg = null; // say hello to RTI server ByteString hello = new ByteString(COCAR_HELLO); packet.setData( hello.getBytes()); socket.send(packet); // receive traffic data while this thread is alive while(isAlive) { packet = new DatagramPacket(new byte[256], 256); socket.receive(packet); System.err.println("received packet!"); byte[] bytes = packet.getData(); binaryMsg = BinaryMessage.parseRequest(bytes); clientService.receiveMessage(binaryMsg); } System.err.println("while loop stopped"); socket.close(); } catch ... } } public void kill() { isAlive = false; } } ************************** ClientService public class ClientService extends Service { private final String COCAR_POST = "COCAR POST"; private ReceiveThread receiveThread; @Override public IBinder onBind(Intent arg0) { // TODO Auto-generated method stub return null; } @Override public void onCreate() { super.onCreate(); receiveThread = new ReceiveThread(this); // debug Toast.makeText(this, "CoCar Client service created", Toast.LENGTH_LONG).show(); } @Override public void onStart(Intent intent, int startId) { super.onStart(intent, startId); receiveThread.start(); Toast.makeText(this, "CoCar Client service started, receiving traffic events", Toast.LENGTH_LONG).show(); } @Override public void onDestroy() { super.onDestroy(); receiveThread.kill(); System.err.println("sent kill to receive thread"); Toast.makeText(this, "CoCar Client service stopped, not receiving traffic events", Toast.LENGTH_LONG).show(); } // receives a binary message from the receive thread and acts accordingly public void receiveMessage(BinaryMessage binaryMsg) { if( (binaryMsg.getCocarDirective().getText().toUpperCase().equals (COCAR_POST)) || binaryMsg.getCocarDirective().substring(0,10).getText().toUpperCase ().equals(COCAR_POST)) { createTrafficMessage(binaryMsg); // TODO: add new message to overlay, update map etc } } // creates a traffic message from a binary one private TrafficMessage createTrafficMessage(BinaryMessage binaryMsg) { TrafficMessage trafficMsg = new TrafficMessage(binaryMsg); // debug trafficMsg.printToLog(); return trafficMsg; } } *************************** Activity public class HelloMapView extends MapActivity implements OnClickListener { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); initMap(); initLocationManager(); createAndShowTrafficMessage(); cocarClient = startService(new Intent(this, ClientService.class)); } @Override public void onPause() { super.onPause(); locManager.removeUpdates(locListener); } @Override // use case: if someone is calling and hangs up, after we get back to our activity, // gps won't be sending the new signals as the locationListener is still removed public void onResume() { super.onResume(); locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locListener); } @Override public void onDestroy() { super.onDestroy(); stopService(new Intent(this, cocarClient.getClass())); System.err.println("stopped service!"); } } Mark Murphy instructed me to use Log instead of Toast, will fix that. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -~----------~----~----~----~------~----~------~--~---