Android 无法获取当前位置

Android 无法获取当前位置,android,currentlocation,Android,Currentlocation,我正在尝试获取当前位置,并将其LAt和LON发送到另一个类。但我的应用程序没有获取当前位置,而是显示最后一个位置。请帮助我如何使用GPS和网络提供商获取当前位置 代码如下: public class location implements LocationListener { private LocationManager mgr; private String best; Location location; public static double myLocationLatitude;

我正在尝试获取当前位置,并将其LAt和LON发送到另一个类。但我的应用程序没有获取当前位置,而是显示最后一个位置。请帮助我如何使用GPS和网络提供商获取当前位置

代码如下:

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();
    }
}
} 
从以下文件:

返回一个位置,该位置指示来自最后一个已知位置的数据 从给定提供程序获取的修复程序

这可以在不启动提供程序的情况下完成。请注意 位置可能已过期,例如,如果设备已关闭 关闭并移动到另一个位置

如果提供程序当前已禁用,则返回null

这意味着
getLastKnownLocation()
将根据
提供程序的最后一次修复来检索
位置。如果要更新
位置
,请使用
onLocationChanged()

中收到的
位置

LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
// Creating a criteria object to retrieve provider
Criteria criteria = new Criteria();
// Getting the name of the best provider
String provider = locationManager.getBestProvider(criteria, true);
if (provider != null) {

location = locationManager.getLastKnownLocation(provider);
// locationManager.requestLocationUpdates(provider, 1000*60*60,
// 1000, this);
// update only in 1 hour and 1 km of distance
locationManager.requestLocationUpdates(provider, 100, 1, this);
}