質問

I've been testing LocationListener (http://developer.android.com/reference/android/location/LocationListener.html), and implemented the abstract onLocationChange.

In the onLocationChange method I've modofied the TextViews in the main activity to show the actual coordinates changes.

Once the activity starts and after the GPS has started the connection, the TextViews updates corectly on every coordinate change.

  • Still I'm wondering how this is possibile since I've not implemented an AsyncTask or a secondary thread targeted on the TextViews?
  • If the process is not asynchronous how are TextViews updated every time?
  • Shoudn't I see just one update of the TextView or worse none?

This is my code:

package com.example.androidgps_get_coordinates;

import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {


    TextView textView_lat;
    TextView textView_long;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textView_lat=(TextView)findViewById(R.id.textV_lat);
        textView_long=(TextView)findViewById(R.id.textV_long);


        String serviceString=Context.LOCATION_SERVICE;
        LocationManager locationManager;    //remember to import LocationManager
        locationManager=(LocationManager)getSystemService(serviceString);


         locationManager.requestLocationUpdates(
                    LocationManager.GPS_PROVIDER, 0, 0, 
                    new LocationListener() {
                      public void onLocationChanged(Location location) {
                          textView_lat.setText(String.valueOf(location.getLatitude()));
                          textView_long.setText(String.valueOf(location.getLongitude()));
                      }
                      public void onProviderDisabled(String provider) {}
                      public void onProviderEnabled(String provider) {}
                      public void onStatusChanged(String provider, int status, 
                                                  Bundle extras) {}
                    }
                  );

        String provider=LocationManager.GPS_PROVIDER;
        Location l= locationManager.getLastKnownLocation(provider);
    }
役に立ちましたか?

解決

onLocationChange is called every time you get a location, hence the TextView's will be updated since you are running it in the UI Thread. What makes you thing you need an AsyncTask or a separate Thread?

Another thing though, is a good practice to check for null value of Location before you try to read from it.

他のヒント

You have written listener (LocationListener) and called requestLocationUpdate*s*() It means that you are asking provider to deliver locations periodically - usually every 1 s.

BTW I don't know why did you wrote the line:

Location l= locationManager.getLastKnownLocation(provider);

It does nothing.

onLocationChanged method works like other Listener like onClickListener. When your location change this listener will call and it's like user click a button. So no need to use Thread but if you want save th Locations data or something else it will be important to use Thread. This is my example that use locationListener with thread ,but Thread used only for send data nothing else:

    public void onLocationChanged(final Location location) {
    // TODO Auto-generated method stub
    locationThread = new Thread(new Runnable() {

        public void run() {
            // TODO Auto-generated method stub
            int Status = 0;
            latitudeField=location.getLatitude();
            longitudeField=location.getLongitude();
            Date dt = new Date();
            dt.setDate(dt.getDate());
            Status=CallSoap.CurrentPosition(YarVisitLogin.UserName, YarVisitLogin.Password, YarVisitLogin.Visitor, latitudeField, longitudeField, PersianCalendar.getPersianDate(dt), PersianCalendar.getPersianTime());
            yardb.InsertIntoMyLocations(yardb.getMaxMyLocation_ID()+1, PersianCalendar.getPersianDate(dt), PersianCalendar.getPersianTime(), Double.toString(latitudeField), Double.toString(longitudeField), Status);

        }
    });
    locationThread.start();

}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top