我遇到了这个问题。我有活动和服务 我需要该服务为活动提供gps位置
我的服务类,
public class RecordingService extends Service {
public Thread backgroundWork;
public boolean threadCanRun;
private GPSReceiver _gps;
public void onCreate() {
super.onCreate();
threadCanRun = true;
backgroundWork = new Thread(new Runnable() {
@Override
public void run() {
someTask();
}
});
}
public int onStartCommand(Intent intent, int flags, int startId) {
backgroundWork.start();
return super.onStartCommand(intent, flags, startId);
}
public void onDestroy() {
threadCanRun = false;
super.onDestroy();
}
public IBinder onBind(Intent intent) {
return null;
}
void someTask() {
while (threadCanRun) {
Intent intent = new Intent(RecordingActivity.BROADCAST_ACTION);
_gps = new GPSReceiver(this);
// preluare status GPS
LocationManager myManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
boolean GPSstatus = myManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (GPSstatus) {
_gps.getMyLoction();
try {
TimeUnit.SECONDS.sleep(3);
intent.putExtra("latitude", _gps.getLatitude());
intent.putExtra("longitude", _gps.getLongitude());
intent.putExtra("GPSstatus", GPSstatus);
} catch (InterruptedException e) {
}
sendBroadcast(intent);
} else {
try {
TimeUnit.SECONDS.sleep(1);
intent.putExtra("latitude", 0);
intent.putExtra("longitude", 0);
intent.putExtra("GPSstatus", GPSstatus);
sendBroadcast(intent);
} catch (InterruptedException e) {
}
}
}
}
}
当我尝试调用" _gps.getMyLoction()" 时,应用程序崩溃
获得位置的方法;
public void getMyLoction()
{
_locationManager = (LocationManager) _context.getSystemService(LOCATION_SERVICE);
_isGPSEnabled = _locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (_isGPSEnabled)
{
this._canGetGPSLocation = true;
if (_location == null)
{
_locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0, this);
if (_locationManager != null)
{
_location = _locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
setLocation(_location);
}
}
}
}
也许这不是一个很好的实现从gps获得自动定位,我认为这个解决方案gps甚至在活动的后台工作中给出了位置。你可以帮助我,也许还有另外一个想法,如果这是蝙蝠的想法。
非常感谢