coordinates have to wait half an hour ... how to speed up the process? like all pomanualam did
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="5dp"> <TextView android:id="@+id/tvTitleGPS" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/provider_gps" android:textSize="30sp"> </TextView> <TextView android:id="@+id/tvEnabledGPS" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="24sp"> </TextView> <TextView android:id="@+id/tvStatusGPS" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="24sp"> </TextView> <TextView android:id="@+id/tvLocationGPS" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="24sp"> </TextView> </LinearLayout> activity_main
package com.example.test.location; import java.util.Date; import android.app.Activity; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle; import android.widget.TextView; public class MainActivity extends Activity { TextView tvEnabledGPS; TextView tvStatusGPS; TextView tvLocationGPS; TextView tvEnabledNet; TextView tvStatusNet; TextView tvLocationNet; private LocationManager locationManager; StringBuilder sbGPS = new StringBuilder(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tvEnabledGPS = (TextView) findViewById(R.id.tvEnabledGPS); tvStatusGPS = (TextView) findViewById(R.id.tvStatusGPS); tvLocationGPS = (TextView) findViewById(R.id.tvLocationGPS); locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); } @Override protected void onResume() { super.onResume(); locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000 * 10, 10, locationListener); checkEnabled(); } @Override protected void onPause() { super.onPause(); locationManager.removeUpdates(locationListener); } private LocationListener locationListener = new LocationListener() { @Override public void onLocationChanged(Location location) { showLocation(location); } @Override public void onProviderDisabled(String provider) { checkEnabled(); } @Override public void onProviderEnabled(String provider) { checkEnabled(); showLocation(locationManager.getLastKnownLocation(provider)); } @Override public void onStatusChanged(String provider, int status, Bundle extras) { if (provider.equals(LocationManager.GPS_PROVIDER)) { tvStatusGPS.setText("Status: " + String.valueOf(status)); } } }; private void showLocation(Location location) { if (location == null) return; if (location.getProvider().equals(LocationManager.GPS_PROVIDER)) { tvLocationGPS.setText(formatLocation(location)); } } private String formatLocation(Location location) { if (location == null) return ""; return String.format( "Coordinates: lat = %1$.4f, lon = %2$.4f, time = %3$tF %3$tT", location.getLatitude(), location.getLongitude(), new Date( location.getTime())); } private void checkEnabled() { tvEnabledGPS.setText("Enabled: " + locationManager .isProviderEnabled(LocationManager.GPS_PROVIDER));} } AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.test.location"> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> strings.xml
<resources> <string name="app_name">Location</string> <string name="provider_gps">GPS</string> <string name="provider_network">Network</string> <string name="location_settings">Location settings</string> <string name="btnUpdate">btnUpdate</string> </resources>