android - Service is running in multiple instance -
i have created background service work if application killed. in service getting data sensor. seems service returning sensors value multiple times in second resulting of application getting hanged.
here service:
public class stepcounterservice extends service { private static final string log_tag = "foregroundservice"; public static boolean flag = false; private sensormanager msensormanager; private stepdetector detector; private powermanager mpowermanager; private wakelock mwakelock; @override public ibinder onbind(intent intent) { // todo auto-generated method stub return null; } @override public int onstartcommand(intent intent, int flags, int startid) { /* msensormanager = (sensormanager) this.getsystemservice(sensor_service); msensormanager.registerlistener(detector, msensormanager.getdefaultsensor(sensor.type_accelerometer), sensormanager.sensor_delay_fastest);*/ log.d("zzz", "start command"); return start_sticky; } @override public void oncreate() { // todo auto-generated method stub super.oncreate(); new stepcountmanager(this); log.d("zzz", "create"); flag = true; log.e("service_started", ""); detector = new stepdetector(this); msensormanager = (sensormanager) this.getsystemservice(sensor_service); msensormanager.registerlistener(detector, msensormanager.getdefaultsensor(sensor.type_accelerometer), sensormanager.sensor_delay_fastest); mpowermanager = (powermanager) .getsystemservice(context.power_service); mwakelock = mpowermanager.newwakelock(powermanager.screen_dim_wake_lock | powermanager.acquire_causes_wakeup, "s"); mwakelock.acquire(); reloadsettings(); } @override public void ondestroy() { // todo auto-generated method stub super.ondestroy(); flag = false; if (detector != null) { msensormanager.unregisterlistener(detector); } if (mwakelock != null) { mwakelock.release(); } log.e("service_destroyed", ""); } public void reloadsettings() { if (detector != null) { detector.setsensitivity( float.valueof("10") ); } } @override public void ontaskremoved(intent rootintent) { super.ontaskremoved(rootintent); intent restartservice = new intent(getapplicationcontext(), this.getclass()); restartservice.setpackage(getpackagename()); pendingintent restartservicepi = pendingintent.getservice( getapplicationcontext(), 1, restartservice, pendingintent.flag_one_shot); //restart service once has been killed android // alarmmanager alarmservice = (alarmmanager)getapplicationcontext().getsystemservice(context.alarm_service); // alarmservice.set(alarmmanager.elapsed_realtime, systemclock.elapsedrealtime() +100, restartservicepi); ((alarmmanager) getsystemservice(context.alarm_service)) .set(alarmmanager.rtc, system.currenttimemillis() + 1000, pendingintent .getservice(this, 3, new intent(this, stepcounterservice.class), 0)); } }
here class implementing sensoreventlistener:
public class stepdetector implements sensoreventlistener { public static updatestepcount mstepsupdater; public static int current_setp = 0; public static float sensitivity = 0; //sensitivity private float mlastvalues[] = new float[3 * 2]; private float mscale[] = new float[2]; private float myoffset; private static long end = 0; private static long start = 0; private float mlimit = 10; private float mlastdirections[] = new float[3 * 2]; private float mlastextremes[][] = { new float[3 * 2], new float[3 * 2] }; private float mlastdiff[] = new float[3 * 2]; private int mlastmatch = -1; public stepdetector(context context) { // todo auto-generated constructor stub super(); int h = 480; myoffset = h * 0.5f; mscale[0] = -(h * 0.5f * (1.0f / (sensormanager.standard_gravity * 2))); mscale[1] = -(h * 0.5f * (1.0f / (sensormanager.magnetic_field_earth_max))); } public void setsensitivity(float sensitivity) { mlimit = sensitivity; // 1.97 2.96 4.44 6.66 10.00 15.00 22.50 33.75 50.62 } @override public void onsensorchanged(sensorevent event) { log.d("zzz", "sensor changed"); sensor sensor = event.sensor; // log.i(constant.step_detector, "onsensorchanged"); synchronized (this) { if (sensor.gettype() == sensor.type_orientation) { } else { int j = (sensor.gettype() == sensor.type_accelerometer) ? 1 : 0; if (j == 1) { float vsum = 0; (int = 0; < 3; i++) { final float v = myoffset + event.values[i] * mscale[j]; vsum += v; } int k = 0; float v = vsum / 3; float direction = (v > mlastvalues[k] ? 1: (v < mlastvalues[k] ? -1 : 0)); if (direction == -mlastdirections[k]) { // direction changed int exttype = (direction > 0 ? 0 : 1); // minimum or // maximum? mlastextremes[exttype][k] = mlastvalues[k]; float diff = math.abs(mlastextremes[exttype][k]- mlastextremes[1 - exttype][k]); if (diff > mlimit) { boolean isalmostaslargeasprevious = diff > (mlastdiff[k] * 2 / 3); boolean ispreviouslargeenough = mlastdiff[k] > (diff / 3); boolean isnotcontra = (mlastmatch != 1 - exttype); if (isalmostaslargeasprevious && ispreviouslargeenough && isnotcontra) { end = system.currenttimemillis(); if (end - start > 500) { log.i("step_detector", "current_setp:" + current_setp); current_setp++; mlastmatch = exttype; start = end; } } else { mlastmatch = -1; } } mlastdiff[k] = diff; } mlastdirections[k] = direction; mlastvalues[k] = v; } } } log.d("sensorsteps", string.valueof(current_setp)); new handler().postdelayed(new runnable() { public void run() { if(mstepsupdater!=null){ mstepsupdater.updatestepcount(current_setp); } } }, 1000); } @override public void onaccuracychanged(sensor sensor, int accuracy) { // todo auto-generated method stub }
i want service return sensor data once in second, can store data in db. can tell me how can sensor data every second instead of multiples time in second.
Comments
Post a Comment