由于某些不明原因,我没有获得任何位置,并且未调用onLocationChanged()。
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);
}
};
}
权限是:
<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"
/>
,输出为:
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
当我更改位置设置时,我会看到 onProviderEnabled()来电。
如何获取位置而不是null?
答案 0 :(得分:0)
// 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
更新位置
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
请在您的代码中更新
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000 * 60 * 1, 10, this);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000 * 60 * 1, 10, this)