GPS location update

Thanks for your time and help in advance. I am working with a location using GPS and only I got the current locations and I tried to update the location values ​​but I haven't had any improvement. May I ask you how can I update the location? Code below:

activity.xml:

 <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
        <TextView
                android:id="@+id/prov"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="No provider selected yet"
                android:layout_marginTop="10dp"
                android:textSize="20dp" />
        <TextView
                android:id="@+id/lat"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:text="Latitude: -"
                android:textSize="20dp" />
        <TextView
                android:id="@+id/lon"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:text="Longitude: -"
                android:textSize="20dp" />
    </LinearLayout>

      

MainActivity.java:

package com.example.locationservicetest;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
      TextView latitude;
      TextView longitude;
      TextView provText;
      LocationManager locationManager;
      String provider;
      MyLocationListener mylistener;
      Criteria criteria;

      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          latitude = (TextView) findViewById(R.id.lat);
          longitude = (TextView) findViewById(R.id.lon);
          provText = (TextView) findViewById(R.id.prov);

          //Get the location manager
          locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);          

          //Define the criteria how to select the location provider
          criteria = new Criteria();
          criteria.setAccuracy(Criteria.ACCURACY_FINE);   //default

          //Charge of data?
          criteria.setCostAllowed(false);

          //Get the best provider depending on the criteria
          provider = locationManager.getBestProvider(criteria, false);

          //The last known location of this provider
          Location location = locationManager.getLastKnownLocation(provider);

          mylistener = new MyLocationListener();

          if (location != null) {
              mylistener.onLocationChanged(location);
          } else {
              //leads to the settings because there is no last known location
              Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
              startActivity(intent);
          }
          // location updates: at least 1 meter and 200millsecs change
          locationManager.requestLocationUpdates(provider, 200, 1, mylistener);
      }

      private class MyLocationListener implements LocationListener {     
          @Override
          public void onLocationChanged(Location location) {
            // Initialize the location fields
              latitude.setText("New Latitude: "+String.valueOf(location.getLatitude()));
              longitude.setText("New Longitude: "+String.valueOf(location.getLongitude()));
              provText.setText(provider + " provider has been selected.");

              Toast.makeText(getBaseContext(),"Location changed!",
                        Toast.LENGTH_SHORT).show();
          }

          @Override
          public void onStatusChanged(String provider, int status, Bundle extras) {
              Toast.makeText(getBaseContext(), provider + " status changed to "+status +"!",
                        Toast.LENGTH_SHORT).show();
          }

          @Override
          public void onProviderEnabled(String provider) {
              Toast.makeText(getBaseContext(), "Provider " + provider + " enabled!",
                Toast.LENGTH_SHORT).show();

          }

          @Override
          public void onProviderDisabled(String provider) {
              Toast.makeText(getBaseContext(), "Provider " + provider + " disabled!",
                Toast.LENGTH_SHORT).show();
          }
      }
}

      

I changed AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.locationservicetest"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.locationservicetest.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

      

The logarithm says:

05-20 20:29:40.375: D/ResourcesManager(16834): creating new AssetManager and set to /data/app/com.example.locationservicetest-2/base.apk
05-20 20:29:40.475: D/Activity(16834): performCreate Call secproduct feature valuefalse
05-20 20:29:40.475: D/Activity(16834): performCreate Call debug elastic valuetrue
05-20 20:29:40.495: D/OpenGLRenderer(16834): Render dirty regions requested: true
05-20 20:29:40.545: I/Adreno-EGL(16834): <qeglDrvAPI_eglInitialize:410>: EGL 1.4 QUALCOMM build:  ()
05-20 20:29:40.545: I/Adreno-EGL(16834): OpenGL ES Shader Compiler Version: E031.25.01.03
05-20 20:29:40.545: I/Adreno-EGL(16834): Build Date: 10/28/14 Tue
05-20 20:29:40.545: I/Adreno-EGL(16834): Local Branch: LA.BF.1.1_RB1_20141028_021_patches2
05-20 20:29:40.545: I/Adreno-EGL(16834): Remote Branch: 
05-20 20:29:40.545: I/Adreno-EGL(16834): Local Patches: 
05-20 20:29:40.545: I/Adreno-EGL(16834): Reconstruct Branch: 
05-20 20:29:40.545: I/OpenGLRenderer(16834): Initialized EGL, version 1.4
05-20 20:29:40.565: I/OpenGLRenderer(16834): HWUI protection enabled for context ,  &this =0xafb22088 ,&mEglDisplay = 1 , &mEglConfig = 8 
05-20 20:29:40.565: D/OpenGLRenderer(16834): Enabling debug mode 0
05-20 20:29:40.655: I/Timeline(16834): Timeline: Activity_idle id: android.os.BinderProxy@16c2c9f9 time:221858392

      

I appreciate your guidance, help and time. Thank.

+3
android


source to share


No one has answered this question yet

Check out similar questions:

757
What's the easiest and most reliable way to get the current location of a user on Android?
683
How to get current GPS location programmatically in Android?
572
ViewPager PagerAdapter does not update View
566
Updating Eclipse with Android Development Tools v. 23
451
Save bitmap to location
415
React Native android build failed. SDK location not found
412
How to emulate GPS location in Android emulator?
366
Error [INSTALL_FAILED_ALREADY_EXISTS] when I tried to update the app
286
Update ViewPager dynamically?
0
Using Tabs, Wifi and Gps



All Articles
Loading...
X
Show
Funny
Dev
Pics