For some unclear reason I am not getting any locations, and onLocationChanged() is not invoked.
package com.example.gpsexample;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.Menu;
public class GPS_Location extends Activity implements LocationListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("~~~","~~~ GPS_Location onCreate");
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
Location l1 = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Location l2 = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
Log.d("~~~","~~~ GPS_Location getLastKnownLocation ==> "+l1+" "+l2);
r.run();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onLocationChanged(Location location) {
Log.d("~~~","~~~ onLocationChanged "+location);
int latitude = (int) (location.getLatitude());
int longitude = (int) (location.getLongitude());
Log.i("~~~", "### Latitude: " + latitude + ", Longitude: " + longitude+"\n\n\n###");
}
@Override
public void onProviderDisabled(String provider) {
Log.d("~~~","~~~ onProviderDisabled"+provider);
}
@Override
public void onProviderEnabled(String provider) {
Log.d("~~~","~~~ onProviderEnabled "+provider);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.d("~~~","~~~ "+provider+" "+status+" "+extras);
}
final Handler handler = new Handler();
Runnable r = new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Log.d("~~~","~~~ GPS_Location run");
Location l = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Log.d("~~~","GPS enabled: "+locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER));
Log.d("~~~","~~~ GPS_Location getLastKnownLocation ==> "+l);
l = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
Log.d("~~~","Network enabled: "+locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER));
Log.d("~~~","~~~ Network_Location getLastKnownLocation ==> "+l);
handler.postDelayed(this, 5000);
}
};
}
the permissions are:
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
/>
<uses-permission
android:name="android.permission.ACCESS_COARSE_LOCATION"
/>
<uses-permission
android:name="android.permission.ACCESS_FINE_LOCATION"
/>
<uses-permission
android:name="android.permission.READ_PHONE_STATE"
/>
<uses-permission
android:name="android.permission.ACCESS_NETWORK_STATE"
/>
<uses-permission
android:name="android.permission.INTERNET"
/>
and the output is:
D/~~~ (29852): ~~~ GPS_Location run
D/~~~ (29852): GPS enabled: true
D/~~~ (29852): ~~~ GPS_Location getLastKnownLocation ==> null
D/~~~ (29852): Network enabled: true
D/~~~ (29852): ~~~ Network_Location getLastKnownLocation ==> null
When I change the location settings, I do see onProviderEnabled() calls.
How do I get a location instead of null?
// The minimum distance to change Updates in meters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters
// The minimum time between updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute
Update Location
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Please Update In Your Code
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000 * 60 * 1, 10, this);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000 * 60 * 1, 10, this)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.