Advertisement
Guest User

Android GPS Thread

a guest
May 20th, 2013
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.49 KB | None | 0 0
  1. package com.example.backgroundgps;
  2.  
  3. import java.util.Map;
  4.  
  5. import android.app.Activity;
  6. import android.content.Context;
  7. import android.location.Criteria;
  8. import android.location.Location;
  9. import android.location.LocationListener;
  10. import android.location.LocationManager;
  11. import android.os.Bundle;
  12. import android.os.Handler;
  13. import android.os.HandlerThread;
  14. import android.os.Message;
  15. import android.util.Log;
  16. import android.view.View;
  17. import android.view.View.OnClickListener;
  18. import android.widget.Button;
  19. import android.widget.TextView;
  20.  
  21. public class GPSActivity extends Activity {
  22.     TextView txt;
  23.     String locationProvider = "";
  24.     Location mLocation;
  25.     Button button;
  26.     private final LocationListener mLocationListener = new LocationListener() {
  27.            
  28.             @Override
  29.             public void onLocationChanged(Location location) {
  30.                     Log.i("TestGPSOnSecondThread", "onLocationChanged, location = " + location);
  31.             }
  32.            
  33.             @Override
  34.             public void onProviderDisabled(String provider) {
  35.             }
  36.            
  37.             @Override
  38.             public void onProviderEnabled(String provider) {
  39.             }
  40.            
  41.             @Override
  42.             public void onStatusChanged(String provider, int status, Bundle extras) {
  43.             }
  44.            
  45.     };
  46.    
  47.     HandlerThread mThread;
  48.     int clicks=0;
  49.     private LocationManager mLocationManager;
  50.  
  51.    
  52. Handler mHandler = new Handler() {
  53.         @Override
  54.         public void handleMessage(Message msg) {
  55.             String text = (String)msg.obj;
  56.             txt.setText(text);
  57.             //call setText here
  58.         }
  59. };
  60. @Override
  61. public void onCreate(final Bundle savedInstanceState) {
  62.     super.onCreate(savedInstanceState);
  63.     setContentView(R.layout.activity_gps);
  64.     txt=(TextView)findViewById(R.id.TextView02);
  65.     button=(Button) findViewById(R.id.button1);
  66.     mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
  67.    
  68.         //Toast.makeText(getApplicationContext(),"Running",Toast.LENGTH_SHORT).show();
  69.    
  70.     button.setOnClickListener(new OnClickListener() {  
  71.         @Override
  72.         public void onClick(View v) {
  73.             // TODO Auto-generated method stub
  74.            
  75.             mThread = new HandlerThread("GPS Thread");
  76.             Log.v("baaga",mThread.getState().toString());
  77.             mThread.start();
  78.             new Handler(mThread.getLooper()).post(
  79.                     new Runnable() {
  80.                             @Override
  81.                             public void run() {
  82.                                 clicks++;
  83.                                     mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mLocationListener);
  84.                                     Criteria criteria = new Criteria();
  85.                                     criteria.setAccuracy(Criteria.ACCURACY_FINE);
  86.  
  87.                                     // Select Best Provider-Like Wifi, Network and GPS.
  88.                                     locationProvider = mLocationManager.getBestProvider(criteria, true);
  89.                                    
  90.                                    
  91.                                     // Save the last known location
  92.                                     mLocation = mLocationManager.getLastKnownLocation(locationProvider);
  93.                                     Message msg = new Message();
  94.                                     String textTochange="";
  95.                                     if (clicks==1){
  96.                                         textTochange =""+ mLocation.getLatitude()+" "+(mLocation.getLongitude()+"baaga"+clicks);
  97.                                     }
  98.                                     else if (clicks==2){
  99.                                         textTochange =""+ mLocation.getLatitude()+" "+mLocation.getLongitude()+"beega"+clicks;
  100.                                     }
  101.                                     else {
  102.                                         textTochange =""+ mLocation.getLatitude()+" "+mLocation.getLongitude()+"booga"+clicks;
  103.                                     }
  104.                                    
  105.                                     msg.obj = textTochange;
  106.                                     mHandler.sendMessage(msg);
  107.                                    
  108.                                     //txt.setText(""+mLocation.getLatitude()+" "+mLocation.getLongitude());
  109.                             }
  110.                     });
  111.            
  112.         }
  113.     });
  114. }
  115.     @Override
  116.     protected void onPause() {
  117.             super.onPause();
  118.             //mLocationManager.removeUpdates(mLocationListener);
  119.             //mThread.getLooper().quit();
  120.     }
  121.  
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement