1. Download our Official Android App: Forums for Android!

Apps How to Display GPS Coordinates from AysncTask in UI of MainActivity - Need Help Please!

Discussion in 'Android Development' started by ac4android, Jun 21, 2016.

  1. ac4android

    ac4android Well-Known Member
    Thread Starter
    Rank:
    None
    Points:
    53
    Posts:
    198
    Joined:
    Nov 3, 2015

    Nov 3, 2015
    198
    29
    53
    My Java is failing me again! :mad:

    I need to have a locationlistener running continuously in the background to update the last known location. To this end, I spawned an Asynctask to forestall possible blockings.

    But I am having difficulties trying to display the new latitude and longitude in my coordinates field in the UI-thread i.e. in the MainAcitivity.

    I have reduced the codes to the simplest to isolate the problem. Here the are.
    Code (Text):
    1.  
    2. content_main.xml
    3. <?xml version="1.0" encoding="utf-8"?>
    4. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    5.   android:orientation="vertical"
    6.   android:layout_width="match_parent"
    7.   android:layout_height="match_parent">
    8.  
    9.   <TextView
    10.   android:id="@+id/textView6"
    11.   android:layout_width="wrap_content"
    12.   android:layout_height="wrap_content"
    13.   android:layout_below="@+id/btnLogin"
    14.   android:layout_marginTop="12dp"
    15.   android:text="Display GPS Coordinates"
    16.   android:textAppearance="?android:attr/textAppearanceSmall"
    17.   android:textSize="12sp"
    18.   android:textColor="@color/colorPrimary"/>
    19.  
    20.   <Button
    21.   android:id="@+id/btnLogin"
    22.   android:layout_width="wrap_content"
    23.   android:layout_height="wrap_content"
    24.   android:text="Log in"
    25.   android:onClick="onClickLogin"
    26.   android:layout_alignParentTop="true"/>
    27.  
    28. </RelativeLayout>
    29.  
    Code for MainActivity.java
    Code (Text):
    1.  
    2.  
    3. import android.app.Activity;
    4. import android.os.Bundle;
    5. import android.view.View;
    6. import android.widget.EditText;
    7. import android.widget.TextView;
    8.  
    9. public class MainActivity extends Activity {
    10.  
    11.   private TextView coordinates;
    12.  
    13.   @Override
    14.   protected void onCreate(Bundle savedInstanceState) {
    15.   super.onCreate(savedInstanceState);
    16.   setContentView(R.layout.activity_main);
    17.  
    18.   coordinates = (TextView)findViewById(R.id.textView6);
    19.  
    20.   }
    21.   public void onClickLogin(View view){
    22.   new RefreshCoordinates(this,coordinates).execute();
    23.   }
    24. }
    25.  
    Code for activity RefreshCoordinates.java
    Code (Text):
    1.  
    2. import android.os.AsyncTask;
    3. import android.location.Location;
    4. import android.location.LocationListener;
    5. import android.location.LocationManager;
    6. import android.content.Context;
    7. import android.os.Bundle;
    8. import android.util.Log;
    9. import android.widget.EditText;
    10. import android.widget.TextView;
    11.  
    12.  
    13.  
    14. public class RefreshCoordinates extends AsyncTask<String,Void,String> {
    15.   public TextView coordinatesField;
    16.   public Context context;
    17.  
    18.   public LocationManager locationmanager = null;
    19.   public MyLocationListener mylocationlistener = null;
    20.  
    21.   public double latitude = 0.0;
    22.   public double longitude = 0.0;
    23.  
    24.   public String latlong;
    25.  
    26.   public RefreshCoordinates(Context context, TextView coordinatesField) {
    27.   this.context = context;
    28.   this.coordinatesField = coordinatesField;
    29.   }
    30.  
    31.   protected void onPreExecute() {
    32.   mylocationlistener = new MyLocationListener();
    33.   locationmanager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    34.   locationmanager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, mylocationlistener);
    35.   //this.coordinatesField.setText("New coordinates : " + latlong);
    36.   }
    37.   protected String doInBackground(String... params) {
    38.   // TODO
    39.   while (this.latitude == 0.0 || this.longitude == 0.0) {
    40.   }
    41.   return latlong;
    42.   }
    43.  
    44.   @Override
    45.   public void onPostExecute(String latlong) {
    46.   this.coordinatesField.setText( latlong);     // <<<<<<<<<< I tried to do this to display the ne coordinates, does not work
    47.   }
    48. }
    49.  
    50. /***** class to refresh GPS coordinates *****/
    51. class MyLocationListener implements LocationListener {
    52.   //@Override
    53.   public void onLocationChanged(Location loc) {
    54.   double latitude = (double) loc.getLatitude();
    55.   double longitude = (double) loc.getLongitude();
    56.   // String provider = loc.getProvider();
    57.  
    58.   try {
    59.   latitude = loc.getLatitude();
    60.   longitude = loc.getLongitude();
    61.   } catch(Exception ex){
    62.   ex.printStackTrace();
    63.   }
    64.  
    65.   String latlong  = "LLatitude: " + String.valueOf(latitude) + "  Longitude: " +String.valueOf(longitude);  // <<<<<<<<<< Display this in cooridnatesField on UI!
    66.  
    67.   }
    68.   @Override
    69.   public void onStatusChanged(String provider, int status, Bundle extras) {
    70.   Log.i("OnProviderDisabled", "onProviderDisabled");
    71.   }
    72.   @Override
    73.   public void onProviderDisabled(String provider) {
    74.   Log.i("onProviderEnabled", "onProviderEnabled");
    75.   }
    76.   @Override
    77.   public void onProviderEnabled(String provider) {
    78.   Log.i("onStatusChanged", "onStatusChanged");
    79.   }
    80. }
    81.  
     

    Advertisement

  2. LV426

    LV426 I say we take off and nuke this place from orbit
    Moderator
    Rank:
     #17
    Points:
    1,488
    Posts:
    6,720
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    6,720
    9,450
    1,488
    Male
    Software developer
    South West of England
    Use the solution provided in this question. He's even using a TextView like you want to do.

    http://stackoverflow.com/questions/...asynctask-if-asynctask-is-in-a-separate-class

    In short, you should be passing the Activity into your AsyncTask class constructor, not your TextView

    And using a WeakReference means that the Activity reference won't cause a memory leak. Neat!

    EDIT: I actually prefer the interface solution, as proposed by user Ams. It provides loose coupling between the classes, which is always a good thing.
     
    #2 LV426, Jun 22, 2016
    Last edited: Jun 22, 2016

Share This Page

Loading...