accelerometer - Android Sensors - Problem getting Proximity Sensor Values and problems with using different phones -
have done android dev in past has been while - want build app uses orientation , proximity sensors. so...
msensormanager = (sensormanager) getsystemservice(sensor_service); msensormanager.registerlistener(this, msensormanager.getdefaultsensor(sensor.type_all) , sensormanager.sensor_delay_game);
so set listener - listened sensors - wonder should listen two? (would save battery?)
sensor mysensor = event.sensor; // if (mysensor.gettype() != sensor.type_orientation) return; if (mysensor.gettype() == sensor.type_orientation) { float x = event.values[0]; float y = event.values[1]; float z = event.values[2]; tv.settext("rotation around x, azimuth = " + x); tv1.settext("rotation around y, pitch = " + y); tv2.settext("rotation around z, roll = " + z); } if (mysensor.gettype() == sensormanager.sensor_proximity) { float p = event.values[0]; tv3.settext("proximity = " + p); }
then in onsensorchanged - see above
on lg optimus 1 can display values in textviews of orientation sensor (this phone doesn't have proximity sensor)
but running same code on nexus 1 leads lovely blank screen!?
sorry long question! , if need more infor ask, lg phone 2.2 nexus 1 2.2.2 thanks,
david
you can use same listener sensors. if want listen multiple or sensors, need rewrite line:
msensormanager.registerlistener(this, msensormanager.getdefaultsensor(sensor.type_all) , sensormanager.sensor_delay_game);
because actual implementation of getdefaultsensor(int type)
returns 1 sensor, seen in source code:
public sensor getdefaultsensor(int type) { // todo: need smarter, now, return 1st sensor list<sensor> l = getsensorlist(type); return l.isempty() ? null : l.get(0); } public list<sensor> getsensorlist(int type) { // cache returned lists first time list<sensor> list; final arraylist<sensor> fulllist = sfullsensorslist; synchronized(fulllist) { list = ssensorlistbytype.get(type); if (list == null) { if (type == sensor.type_all) { list = fulllist; } else { list = new arraylist<sensor>(); (sensor : fulllist) { if (i.gettype() == type) list.add(i); } } list = collections.unmodifiablelist(list); ssensorlistbytype.append(type, list); } return list; }
so in case of sensor.type_all
method getsensorlist(int type)
provides (unordered) list of sensors available , getdefaultsensor(int type)
returns first 1 of it.
because list containing sensors unordered, getdefaultsensor(sensor.type_all) supplies random sensor. tried , first got accelerometer , next time light sensor. since handle orientation , proximity changes in listener, suspect getdefaultsensor(sensor.type_all) supplied different sensor on nexus.
you should receive list of sensors available msensormanager.getsensorlist(sensor.type_all)
, iterate on list, in order register every contained sensor respectively.
regarding question saving battery - don't know, if sensors running permanently. if case, 1 can save battery power reducing unnecessary processing. if not need information specific sensor provides, not register receive messages it. reduces messages being sent application.
Comments
Post a Comment