我正在尝试获取当前位置并将其 LAt 和 LON 发送到另一个类。但是我的应用程序没有获取当前位置,它显示了 LAST LOCATION。请帮助我了解如何使用 GPS 和 NETWORK 提供程序获取当前位置.
下面是代码:
public class location implements LocationListener
{
private LocationManager mgr;
private String best;
Location location;
public static double myLocationLatitude;
public static double myLocationLongitude;
public void locUpdate(Context context)
{
mgr = (LocationManager) context.getSystemService(context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
// criteria.setAccuracy(Criteria.ACCURACY_COARSE);
best = mgr.getBestProvider(criteria, true);
mgr.requestLocationUpdates(LocationManager.PASSIVE_PROVIDER,0,0,this);
try{
Location location = mgr.getLastKnownLocation(best);
Intent i = new Intent();
i.setClass(context,sentsms.class);
i.putExtra("latitude", location.getLatitude());
i.putExtra("longitude", location.getLongitude());
i.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// Launch the new activity and add the additional flags to the intent
context.getApplicationContext().startActivity(i);
}
catch(Exception e)
{
}
}
public void onLocationChanged(Location location) {
dumpLocation(location);
}
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
protected void onPause()
{
// super.onPause();
mgr.removeUpdates(this);
}
protected void onResume() {
// super.onResume();
mgr.requestLocationUpdates(best, 15000, 10, this);
}
private void dumpLocation(Location l) {
if (l != null){
myLocationLatitude = l.getLatitude();
myLocationLongitude = l.getLongitude();
}
}
}