A GPS Listner有问题。当我使用LocationManager.GPS_PROVIDER应用程序时,工作时间很短,然后开始没有响应(ANR)。当我只使用LocationManager.NETWORK时,一切都很好。我需要AndroidManifes.xml (我认为是这样)的权限和线路。这是我的代码:
BackgroundGPSService.java
public class BackgroundGPSService extends Service implements GPSLocationListner {
private GPSService service;
public class LocalBinder extends Binder {
public BackgroundGPSService getService() {
return BackgroundGPSService.this;
}
}
private final IBinder localBinder = new LocalBinder();
@Override
public IBinder onBind(Intent arg0) {
return localBinder;
}
@Override
public void onCreate() {
super.onCreate();
service = new GPSService(this);
service.construct();
service.setGPSLocationListner(this);
}
@Override
public void onDestroy() {
super.onDestroy();
service.destory();
}
@Override
public void onUpdateSetteliteCount(int count) {
// TODO Auto-generated method stub
}
@Override
public void onFirstFix() {
// TODO Auto-generated method stub
}
@Override
public void onLocationUpdate(double lat, double lon) {
// TODO Auto-generated method stub
}
}这是我的GPSService,实现了GpsStatus.Listener。
GPSService.java
public class GPSService implements GpsStatus.Listener {
private Context context;
private FineLocationListner fineListner;
private GPSLocationListner listner;
private GPSInfoStruct gpsInfo = new GPSInfoStruct();
public GPSService(Context context) {
this.context = context;
}
public void construct() {
LocationManager locationManager = getLocationManager();
fineListner = new FineLocationListner();
coarseListner = new CoarseLocationListner();
List<String> providerList = (ArrayList<String>)locationManager.getAllProviders();
for(int i=0; i<providerList.size(); i++) {
String providerName = providerList.get(i);
if(DEBUG) Log.d(TAG, "PROVIDERS LIST name: " + providerName + " enable: " + locationManager.isProviderEnabled(providerName));
if(providerName.equals(LocationManager.GPS_PROVIDER))
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 60000, 10, fineListner);
Location loc = locationManager.getLastKnownLocation(providerList.get(i));
gpsInfo.updateLocation(loc);
}
locationManager.addGpsStatusListener(this);
}
public Location getLocation() {
return gpsInfo.getLocation();
}
public double getLat() {
return gpsInfo.getLatitude();
}
public double getLon() {
return gpsInfo.getLongitude();
}
@Override
public void onGpsStatusChanged(int event) {
if(event == GpsStatus.GPS_EVENT_SATELLITE_STATUS) {
GpsStatus status = getLocationManager().getGpsStatus(null);
int counter = 0;
Iterable<GpsSatellite> satellites = status.getSatellites();
Iterator<GpsSatellite> satIter = satellites.iterator();
while(satIter.hasNext()) {
counter++;
}
//if(DEBUG) Log.d(TAG, "THIS onGpsStatusChanged: " + event + " cnt: " + counter);
gpsInfo.setSetteliteCount(counter);
if(listner != null)
listner.onUpdateSetteliteCount(counter);
} else if(event == GpsStatus.GPS_EVENT_FIRST_FIX) {
if(DEBUG) Log.d(TAG, "THIS onFirstFix");
gpsInfo.setFix();
if(listner != null)
listner.onFirstFix();
} else
if(DEBUG) Log.d(TAG, "THIS onGpsStatusChanged: " + event);
}
public void setGPSLocationListner(GPSLocationListner listner) {
this.listner = listner;
if(gpsInfo.isFix()) listner.onFirstFix();
listner.onUpdateSetteliteCount(gpsInfo.getSetteliteCount());
listner.onLocationUpdate(gpsInfo.getLatitude(), gpsInfo.getLongitude());
}
public void destory() {
getLocationManager().removeUpdates(fineListner);
getLocationManager().removeUpdates(coarseListner);
getLocationManager().removeGpsStatusListener(this);
}
private LocationManager getLocationManager() {
return (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
}
class FineLocationListner implements LocationListener {
///implementation
}在LoginActuivity中,我启动服务
public class LoginActivity extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login_activity);
startService(new Intent(this, BackgroundGPSService.class));
}
}我漏掉了什么?
编辑1
当我评论这一行时:
locationManager.addGpsStatusListener(this);应用程序很好。我认为这是个问题,但为什么呢?
发布于 2013-12-04 10:28:52
Google发布新位置API.Its相当容易,并且编写了很少行代码,您就可以得到这个位置。
我已经发了答案,请访问这个链接。
https://stackoverflow.com/questions/20372513
复制相似问题