【发布时间】:2014-03-06 11:37:41
【问题描述】:
在查看有关 Stack Overflow 的一些文章后,我无法找到我的问题的解决方案。像往常一样,我总是得到空值作为getLastKnownLocation 的回报。我也实现了 LocationListener 。下面是代码。
为了您的信息,我已经在下面的代码中检查了两个提供商(网络和 GPS),它们总是返回 false,但是如果我检查 sBestProvider 的值,那么我会得到“网络”。怎么来的?
权限。
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
JAVA代码
public class ActivityMarkingLocation extends Activity implements LocationListener {
GoogleMap oMap ;
LocationManager oLocationManager;
ConnectionDetector oConnectionDetector ;
Location oLocation;
Button oBtnGetCurrentLocation;
String sBestProvider;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_marking_location);
oLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria oCriteria = new Criteria();
sBestProvider = oLocationManager.getBestProvider(oCriteria, false);
boolean b =oLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
System.out.println("GPS Status " + b);
boolean c = oLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
System.out.println("Network Status " + c);
System.out.println("Provider " + sBestProvider);
oLocation = oLocationManager.getLastKnownLocation(sBestProvider);
System.out.println("Provider : " + sBestProvider);
System.out.println("LocationManager : " + oLocationManager);
if (oLocation == null)
System.out.println("Location Null " );
else
System.out.println("Location not null");
oBtnGetCurrentLocation = (Button) findViewById(R.id.btnGetCurrentLocation);
oBtnGetCurrentLocation.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
oLocation = oLocationManager.getLastKnownLocation(sBestProvider);
if (oLocation != null)
System.out.println("Location found");
if (oLocation != null)
System.out.println("Position : " + oLocation.getLatitude() + " " + oLocation.getLongitude());
}
});
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
oLocationManager.requestLocationUpdates(sBestProvider, 600, 10, this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_landing_screen, menu);
return true;
}
public boolean isGoogleMapsInstalled()
{
try
{
ApplicationInfo info = getPackageManager().getApplicationInfo("com.google.android.apps.maps", 0 );
return true;
}
catch(PackageManager.NameNotFoundException e)
{
return false;
}
}
@Override
public void onLocationChanged(Location arg0) {
// TODO Auto-generated method stub
oLocation = arg0;
System.out.println("Location changed");
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
【问题讨论】:
-
为什么我的代码不起作用?
-
解决了,我的错。设置中未勾选“使用无线网络”。感谢所有花费宝贵时间为我提供解决方案的人。