good evening everyone,
i'm doing application values of different sensors accelerometer, proximity sensor, compass , gps, values i'm going use robotics project. yet, have problem gps doesn't update when change location without closing activity. indeed, if stop application , restart @ place, coords correct, that's problem of updating. please notice location being recalculated when screen change orientation (landscape -> portrait). work without touching anything, hehe.
so here code :
package com.pindus.sensors; import android.app.activity; import android.content.context; import android.hardware.sensor; import android.hardware.sensorevent; import android.hardware.sensoreventlistener; import android.hardware.sensormanager; import android.location.location; import android.location.locationlistener; import android.location.locationmanager; import android.os.bundle; import android.widget.textview; public class sensors extends activity implements sensoreventlistener{ sensormanager sm; locationmanager lm; locationlistener ls; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); sm = (sensormanager) getsystemservice(sensor_service); lm = (locationmanager)getsystemservice(context.location_service); boolean accelsupported = sm.registerlistener(this, sm.getdefaultsensor(sensor.type_accelerometer),sensormanager.sensor_delay_fastest); if(!accelsupported) { sm.unregisterlistener(this, sm.getdefaultsensor(sensor.type_accelerometer)); ((textview)findviewbyid(r.id.acc)).settext("accéléromètre non disponible"); } boolean compasssupported = sm.registerlistener(this, sm.getdefaultsensor(sensor.type_orientation),sensormanager.sensor_delay_fastest); if(!compasssupported) { sm.unregisterlistener(this, sm.getdefaultsensor(sensor.type_orientation)); ((textview)findviewbyid(r.id.compass)).settext("boussole non disponible"); } boolean proxysupported = sm.registerlistener(this, sm.getdefaultsensor(sensor.type_proximity),sensormanager.sensor_delay_fastest); if(!proxysupported) { sm.unregisterlistener(this, sm.getdefaultsensor(sensor.type_proximity)); ((textview)findviewbyid(r.id.proxy)).settext("capteur de proximité non disponible"); } ls = new mylocationlistener(); lm.requestlocationupdates( locationmanager.network_provider, 0, 0, ls); } @override public void onstop() { super.onstop(); lm.removeupdates(ls); sm.unregisterlistener(this); } @override public void onpause() { super.onstop(); lm.removeupdates(ls); sm.unregisterlistener(this); } @override public void onaccuracychanged(sensor sensor, int accuracy) { // todo auto-generated method stub } @override public void onsensorchanged(sensorevent event) { switch(event.sensor.gettype()) { case sensor.type_accelerometer: onaccelchanged(event); break; case sensor.type_orientation: oncompasschanged(event); break; case sensor.type_proximity: onproxychanged(event); break; } } public void onaccelchanged(sensorevent event) { float ax,ay,az; ax = event.values[0]; ay = event.values[1]; az = event.values[2]; ((textview)findviewbyid(r.id.axex)).settext("axe x : " + ax); ((textview)findviewbyid(r.id.axey)).settext("axe y : " + ay); ((textview)findviewbyid(r.id.axez)).settext("axe z : " + az); } public void oncompasschanged(sensorevent event) { float azimuth,pitch,roll; azimuth = event.values[0]; pitch = event.values[1]; roll = event.values[2]; ((textview)findviewbyid(r.id.azimuth)).settext("azimuth : " + azimuth); ((textview)findviewbyid(r.id.pitch)).settext("pitch : " + pitch); ((textview)findviewbyid(r.id.roll)).settext("roll : " + roll); } public void onproxychanged(sensorevent event) { float x; x = event.values[0]; ((textview)findviewbyid(r.id.prox)).settext("proximité : " + x); } public class mylocationlistener implements locationlistener { @override public void onlocationchanged(location location) { double longitude, lattitude; longitude = location.getlongitude(); lattitude = location.getlatitude(); ((textview)findviewbyid(r.id.longi)).settext("longitude : " + longitude); ((textview)findviewbyid(r.id.latti)).settext("lattitude : " + lattitude); lm.requestlocationupdates( locationmanager.network_provider, 0, 0, ls); } @override public void onproviderdisabled(string provider) { ((textview)findviewbyid(r.id.warngps)).settext("gps desactivé"); } @override public void onproviderenabled(string provider) { ((textview)findviewbyid(r.id.warngps)).settext("gps activé"); } @override public void onstatuschanged(string provider, int status, bundle extras) { // todo auto-generated method stub } }
}
has idea problem ?
thank , have nice day/evening/night.
replace below line
lm.requestlocationupdates(locationmanager.gps_provider, 1000l, 1.0f, ls);
Comments
Post a Comment