我需要帮助使用GPS Logger。我有一个切换按钮。我设置它的地方我需要启动GPS记录器,我需要设置记录器间隔。我需要在sqlite DB中显示纬度和经度,并根据日志间隔更新数据库。
任何有效的示例代码对我都有帮助。我找到了很多样本申请。但没有奏效。任何样本工作链接都会有很大的帮助。
答案 0 :(得分:2)
public class LocalisationService extends Service
{
private LocationManager locationManager;
private BroadcastReceiver receiver;
//Define a listener that responds to location updates
private LocationListener locationListener = new LocationListener()
{
//Called when a new location is found by the network location provider
public void onLocationChanged(Location location)
{
//insertion of the location in the DB
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
};
@Override
public IBinder onBind(Intent intent){return null;}
@Override
public void onCreate()
{
super.onCreate();
int interval = 1; //Specify the update interval
locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
// Register the listener with the Location Manager to receive location updates
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, interval, 0, locationListener);
IntentFilter filter = new IntentFilter();
filter.addAction("STOP_LOCALISATION_SERVICE");
receiver = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{
stopSelf();
}
};
registerReceiver(receiver, filter);
}
@Override
public void onDestroy()
{
unregisterReceiver(receiver);
locationManager.removeUpdates(locationListener);
super.onDestroy();
}
}