在 android 2.2 中运行应用程序时,GPS 正在搜索位置,但不显示任何位置。此应用程序在 ICS 版本中运行良好。会是什么原因?
提前致谢。
这是完整的代码:-
package com.example.gps;
import android.app.Activity;
import android.location.Criteria;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.Location;
import android.os.Bundle;
import android.view.Window;
import android.widget.TextView;
import java.io.IOException;
public class GPSmap extends Activity implements LocationListener {
public String mLatLongString = "";
private String mBest;
private TextView myLocationText;
LocationManager mLocManager;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_LEFT_ICON);
setContentView(R.layout.activity_gpsmap);
myLocationText = (TextView) findViewById(R.id.myLocationText);
getWindow().setFeatureDrawableResource(Window.FEATURE_LEFT_ICON,
R.drawable.ic_launcher);
mLocManager = (LocationManager) getSystemService(LOCATION_SERVICE);
try {
Criteria mCriteria = new Criteria();
mCriteria.setAccuracy(Criteria.ACCURACY_FINE);
mBest = mLocManager.getBestProvider(mCriteria, true);
if (mLocManager.isProviderEnabled(mBest)) {
}
} catch (NullPointerException e) {
myLocationText = (TextView) findViewById(R.id.myLocationText);
myLocationText.setText("\n\nGPS Tracking: Disabled.");
}
}
private void updateWithNewLocation(Location mLocation) {
myLocationText = (TextView) findViewById(R.id.myLocationText);
if (mLocation != null) {
double mLatitude = mLocation.getLatitude();
double mLongitude = mLocation.getLongitude();
double mLatitudeFormatted = (double) Math.round(mLatitude * 100000) / 100000;
double mLongitudeFormatted = (double) Math
.round(mLongitude * 100000) / 100000;
mLatLongString = mLatitudeFormatted + ", " + mLongitudeFormatted;
myLocationText.setText(mLatLongString);
} else {
mLatLongString = "\n\nNo Location Found.";
myLocationText.setText(mLatLongString);
}
}
/** Function to listen to location change */
public void onLocationChanged(Location location) {
updateWithNewLocation(location);
}
public void onProviderDisabled(String provider) {
updateWithNewLocation(null);
}
public void onProviderEnabled(String provider) {}
public void onStatusChanged(String provider, int status, Bundle extras) {}
protected void onResume() {
try {
super.onResume();
mLocManager.removeUpdates((android.location.LocationListener) this);
mLocManager.requestLocationUpdates(mBest, 60000L, 0.0f, this);
} catch (NullPointerException e) {
myLocationText = (TextView) findViewById(R.id.myLocationText);
myLocationText.setText("\n\nGPS Tracking: Disabled.");
}
}
}