Android打开GPS导航并获取位置信息返回null解决方案
时间:2021-08-31 09:21:19
最近在做一个 Android 项目,需要用到GPS获取位置信息,从 API 查了一下,发现获取位置信息仅需极其简单的一句即可:
getLastKnownLocation(LocationManager.GPS_PROVIDER),
于是高兴地不得了。可是一写进代码里,返回值(Location 类型)居然一直为null..郁闷的不得了。在网上查了好久,发现好多人都和我一样纠结于这个问题上,有人说是因为GPS没打开,也有人说是相关权限没加上..可是我的明明已经在设置里打开,权限自然也加上了。在api上纠结了半天,终于找出原因了,原来要打开GPS其实在于这句:
setTestProviderEnabled("gps",true);
而跟手机上的设置没多大关系(起码在我的手机上测是这样的)。手机上的设置关闭了,这一句照样能打开;而即使手机设置打开了,没这一句也是白搭。与这句对应的是
setTestProviderEnabled("gps",false);
用来关闭GPS.
GPS打开后可以用上面的方法获取Location了吗?还是不可以!确切地说是有时候可以,因为这个函数获取的是上次已经获得的位置信息,设想如果此程序第一次跑,先前并没有获取过位置信息,当然返回值为null了。经仔细查看api,在
requestLocationUpdates (String provider, long minTime, float minDistance, LocationListener listener)
里发现了这样一句话:It may take a while to receive the most recent location. If an immediate location is required, applications may use the getLastKnownLocation(String) method. 因此为了获取位置信息,应该用此方法为manager设置 * ,在 * 中onLocationChanged(Location location)里获取。
测试代码如下:
public void onLocationChanged(Location location)
{
Log.i("onLocationChanged", "come in");
if (location != null)
{
Log.w("Location","Current altitude = "+ location.getAltitude());
Log.w("Location","Current latitude = "+ location.getLatitude());
}
}
经过测试,经过一段时间后可以获取Location(获取时间与minTime、minDistance相关)。还需注意的一个问题是在设置了 * 后,删除 * 之前不能用上面的方法关闭gps,否则会报错。因此关闭gps的方法是
manager.removeUpdates (listener);//listener 即为 * 实例
manager.setTestProviderEnabled("gps",false);
以下是测试代码,所需权限有:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.UPDATE_DEVICE_STATS"></uses-permission>
import android.app.Activity;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
public class audio extends Activity
{
/** Called when the activity is first created. */
LocationManager locationManager;
LocationListener llistener;
String provider;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);
String serviceName = Context.LOCATION_SERVICE;
locationManager = (LocationManager) getSystemService(serviceName);
locationManager.setTestProviderEnabled("gps", true);
provider = locationManager.getBestProvider(criteria, true);
Log.d("provider", provider);
llistener = new LocationListener() {
@Override
public void onLocationChanged(Location location)
{
// TODO Auto-generated method stub
Log.i("onLocationChanged", "come in");
if (location != null)
{
Log.w("Location", "Current altitude = "
+ location.getAltitude());
Log.w("Location", "Current latitude = "
+ location.getLatitude());
}
locationManager.removeUpdates(this);
locationManager.setTestProviderEnabled(provider, false);
}
@Override
public void onProviderDisabled(String provider)
{
// TODO Auto-generated method stub
Log.i("onProviderDisabled", "come in");
}
@Override
public void onProviderEnabled(String provider)
{
// TODO Auto-generated method stub
Log.i("onProviderEnabled", "come in");
}
@Override
public void onStatusChanged(String provider, int status,
Bundle extras)
{
// TODO Auto-generated method stub
Log.i("onStatusChanged", "come in");
}
};
locationManager.requestLocationUpdates(provider, 1000, (float) 1000.0, llistener);
}
protected void onDestroy()
{
locationManager.removeUpdates(llistener);
locationManager.setTestProviderEnabled(provider, false);
super.onDestroy();
}