Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.backgroundgps;
- import java.util.Map;
- import android.app.Activity;
- import android.content.Context;
- import android.location.Criteria;
- import android.location.Location;
- import android.location.LocationListener;
- import android.location.LocationManager;
- import android.os.Bundle;
- import android.os.Handler;
- import android.os.HandlerThread;
- import android.os.Message;
- import android.util.Log;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.TextView;
- public class GPSActivity extends Activity {
- TextView txt;
- String locationProvider = "";
- Location mLocation;
- Button button;
- private final LocationListener mLocationListener = new LocationListener() {
- @Override
- public void onLocationChanged(Location location) {
- Log.i("TestGPSOnSecondThread", "onLocationChanged, location = " + location);
- }
- @Override
- public void onProviderDisabled(String provider) {
- }
- @Override
- public void onProviderEnabled(String provider) {
- }
- @Override
- public void onStatusChanged(String provider, int status, Bundle extras) {
- }
- };
- HandlerThread mThread;
- int clicks=0;
- private LocationManager mLocationManager;
- Handler mHandler = new Handler() {
- @Override
- public void handleMessage(Message msg) {
- String text = (String)msg.obj;
- txt.setText(text);
- //call setText here
- }
- };
- @Override
- public void onCreate(final Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_gps);
- txt=(TextView)findViewById(R.id.TextView02);
- button=(Button) findViewById(R.id.button1);
- mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
- //Toast.makeText(getApplicationContext(),"Running",Toast.LENGTH_SHORT).show();
- button.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- mThread = new HandlerThread("GPS Thread");
- Log.v("baaga",mThread.getState().toString());
- mThread.start();
- new Handler(mThread.getLooper()).post(
- new Runnable() {
- @Override
- public void run() {
- clicks++;
- mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mLocationListener);
- Criteria criteria = new Criteria();
- criteria.setAccuracy(Criteria.ACCURACY_FINE);
- // Select Best Provider-Like Wifi, Network and GPS.
- locationProvider = mLocationManager.getBestProvider(criteria, true);
- // Save the last known location
- mLocation = mLocationManager.getLastKnownLocation(locationProvider);
- Message msg = new Message();
- String textTochange="";
- if (clicks==1){
- textTochange =""+ mLocation.getLatitude()+" "+(mLocation.getLongitude()+"baaga"+clicks);
- }
- else if (clicks==2){
- textTochange =""+ mLocation.getLatitude()+" "+mLocation.getLongitude()+"beega"+clicks;
- }
- else {
- textTochange =""+ mLocation.getLatitude()+" "+mLocation.getLongitude()+"booga"+clicks;
- }
- msg.obj = textTochange;
- mHandler.sendMessage(msg);
- //txt.setText(""+mLocation.getLatitude()+" "+mLocation.getLongitude());
- }
- });
- }
- });
- }
- @Override
- protected void onPause() {
- super.onPause();
- //mLocationManager.removeUpdates(mLocationListener);
- //mThread.getLooper().quit();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement