A GPS Listner有问题。当我使用LocationManager.GPS_PROVIDER应用程序应用程序工作几秒钟,然后开始没有响应(ANR)。当我只使用LocationManager.NETWORK时,evrything工作正常。我在AndroidManifes.xml中需要permision和lines(我想是这样)。这是我的代码:
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);
申请工作正常。我认为这是一个问题,但为什么呢? :(
答案 0 :(得分:0)
谷歌Android发布新的位置API。它非常简单,只需编写几行代码即可获得位置
我已经发布了答案,请转到此链接。