Guest User

Untitled

a guest
Mar 13th, 2010
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.71 KB | None | 0 0
  1. package name.nanek.gpsonsecondthread;
  2.  
  3. import android.app.Activity;
  4. import android.content.Context;
  5. import android.location.Location;
  6. import android.location.LocationListener;
  7. import android.location.LocationManager;
  8. import android.os.Bundle;
  9. import android.os.Handler;
  10. import android.os.HandlerThread;
  11. import android.util.Log;
  12.  
  13. public class TestGPSOnSecondThread extends Activity {
  14.  
  15.     private final LocationListener mLocationListener = new LocationListener() {
  16.        
  17.         @Override
  18.         public void onLocationChanged(Location location) {
  19.             Log.i("TestGPSOnSecondThread", "onLocationChanged, location = " + location);
  20.         }
  21.        
  22.         @Override
  23.         public void onProviderDisabled(String provider) {
  24.         }
  25.        
  26.         @Override
  27.         public void onProviderEnabled(String provider) {
  28.         }
  29.        
  30.         @Override
  31.         public void onStatusChanged(String provider, int status, Bundle extras) {
  32.         }
  33.        
  34.     };
  35.    
  36.     private HandlerThread mThread;
  37.  
  38.     private LocationManager mLocationManager;
  39.    
  40.     @Override
  41.     public void onCreate(final Bundle savedInstanceState) {
  42.         super.onCreate(savedInstanceState);
  43.         setContentView(R.layout.main);
  44.         mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
  45.     }
  46.  
  47.     @Override
  48.     protected void onResume() {
  49.         super.onResume();
  50.         mThread = new HandlerThread("GPS Thread");
  51.         mThread.start();
  52.         new Handler(mThread.getLooper()).post(
  53.             new Runnable() {
  54.                 @Override
  55.                 public void run() {
  56.                     mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mLocationListener);
  57.                 }
  58.             });
  59.     }
  60.  
  61.     @Override
  62.     protected void onPause() {
  63.         super.onPause();
  64.         mLocationManager.removeUpdates(mLocationListener);
  65.         mThread.getLooper().quit();
  66.     }
  67.    
  68. }
Advertisement
Add Comment
Please, Sign In to add comment