I´m programming a service to recollect data from several sensors. I do not 
want to overload the system with the service.

I have a sensor data listener service, this recolletc data from several 
sensors and try to save on a each sensor file. The code of the method is in 
below:

*public class SensorPrinterService extends Service implements 
SensorEventListener {*
* **
*
* private final IBinder binder = new SensorPrinterBinder();*
* **
*
* private SensorPrinter [] printers = {*
* new SensorPrinter(Sensor.TYPE_ACCELEROMETER), *
* new SensorPrinter(Sensor.TYPE_GYROSCOPE), *
* new SensorPrinter(Sensor.TYPE_MAGNETIC_FIELD), *
* new SensorPrinter(Sensor.TYPE_ORIENTATION), *
* new SensorPrinter(Sensor.TYPE_LIGHT), *
* new SensorPrinter(Sensor.TYPE_PROXIMITY)*
* };*
* **
*
* private SensorDetection [] sensors = new SensorDetection[printers.length];
*
* **
*
* public IBinder onBind(Intent intent) {*
* return binder;*
* // return the binder(myService reference) when service*
* // connected using Service Connection*
* }*
*
*
* // Instance of the Service is now in SensorPrinterBinder*
* public class SensorPrinterBinder extends Binder {*
* SensorPrinterService getService() {*
* return SensorPrinterService.this;*
* }*
* }*
* **
*
* public void onCreate() {*
* super.onCreate(); *
* **
*
* Log.i(this.getClass().getSimpleName(), "Creating"); *
* **
*
* for(int i = 0; i < printers.length; i++) {*
* **
*
* try {*
* if ( printers[i].open() ) {*
* sensors[i] = new SensorDetection(this.getApplicationContext(), *
* printers[i].getSensorType(), SensorManager.SENSOR_DELAY_GAME, this);*
* **
*
* sensors[i].start();*
* }*
* else { *
* if( printers[i].EXTERNAL_STORAGE_AVAILABLE == false ) {*
* Log.e(this.getClass().getSimpleName(), printers[i].getSensorName() *
* + " - EXTERNAL_STORAGE_AVAILABLE_ERROR");*
* showNotification( this.getString( 
R.string.EXTERNAL_STORAGE_AVAILABLE_ERROR ) );*
* }*
* else {*
* Log.e(this.getClass().getSimpleName(), printers[i].getSensorName() *
* + " - EXTERNAL_STORAGE_WRITEABLE_ERROR");*
* showNotification( this.getString( 
R.string.EXTERNAL_STORAGE_WRITEABLE_ERROR ) );*
* } *
* }*
* } catch (FileNotFoundException e) {*
* Log.e(this.getClass().getSimpleName(), e.toString());*
* showNotification( e.toString() );*
* }*
* **
*
* }*
* **
*
* }*
* **
*
* public void onDestroy() {*
* **
*
* Log.i(this.getClass().getSimpleName(), "Destroying");*
* **
*
* for(int i = 0; i < printers.length; i++) {*
* sensors[i].stop();*
* printers[i].close( true ); *
* }*
* **
*
* super.onDestroy(); *
* **
*
* }*
* **
*
* private void showNotification(String text) {*
* **
*
* NotificationManager notificationManager = (NotificationManager) *
* getSystemService(NOTIFICATION_SERVICE);*
* Notification notification = new Notification(R.drawable.warning, *
* this.getString( R.string.service_error_titleonbar ), *
* System.currentTimeMillis());*
* notification.flags = Notification.FLAG_AUTO_CANCEL;*
* PendingIntent contentIntent = PendingIntent.*
* getActivity(this, 0, new Intent(this, Runner.class), 0);*
*
*
* notification.setLatestEventInfo(this.getApplicationContext(), *
* this.getString( R.string.service_error_title ), *
* text, contentIntent);*
*
*
* int SERVER_DATA_RECEIVED = 1;*
* notificationManager.notify(SERVER_DATA_RECEIVED, notification);*
* **
*
* }*
*
*
* public void onAccuracyChanged(Sensor sensor, int accuracy) {*
* // TODO Auto-generated method stub*
* **
*
* }*
*
*
* public void onSensorChanged(SensorEvent event) {*
* **
*
* switch( event.sensor.getType() ) {*
* case Sensor.TYPE_ACCELEROMETER:*
* **
*
* Log.i(this.getClass().getSimpleName(), "TYPE_GYROSCOPE: " *
* + event.toString());*
* **
*
* synchronized( this ) {*
* printers[0].onSensorChanged(event);*
* }*
* **
*
* break;*
* case Sensor.TYPE_GYROSCOPE:*
*
*
* Log.i(this.getClass().getSimpleName(), "TYPE_GYROSCOPE: " *
* + event.toString());*
* **
*
* synchronized( this ) {*
* printers[1].onSensorChanged(event);*
* }*
* **
*
* break;*
* case Sensor.TYPE_MAGNETIC_FIELD:*
*
*
* Log.i(this.getClass().getSimpleName(), "TYPE_MAGNETIC_FIELD: " *
* + event.toString());*
* **
*
* synchronized( this ) {*
* printers[2].onSensorChanged(event);*
* }*
* **
*
* break;*
* case Sensor.TYPE_ORIENTATION:*
*
*
* Log.i(this.getClass().getSimpleName(), "TYPE_ORIENTATION: " *
* + event.toString());*
* **
*
* synchronized( this ) {*
* printers[3].onSensorChanged(event);*
* }*
* **
*
* break;*
* case Sensor.TYPE_LIGHT:*
* **
*
* Log.i(this.getClass().getSimpleName(), "TYPE_LIGHT: " *
* + event.toString());*
* **
*
* synchronized( this ) {*
* printers[4].onSensorChanged(event);*
* }*
* **
*
* break;*
* case Sensor.TYPE_PROXIMITY:*
*
*
* Log.i(this.getClass().getSimpleName(), "TYPE_PROXIMITY: " *
* + event.toString());*
* **
*
* synchronized( this ) {*
* printers[5].onSensorChanged(event);*
* }*
* **
*
* break;*
* default:*
* break; *
* }*
* **
*
* }*
* **
*
* **
*
*}*

I have a printer object and a sensor detector, the code is in below:

*public class SensorDetection {

private SensorManager sensorManager;
private Sensor sensor;

// Listener of data measures
private SensorEventListener sensorEventListener;
private int sensorType;
private int sensorDelay;
// It is needed to get system service.
private Context parent;

public SensorDetection(Context parent, int sensorType, 
int sensorDelay, SensorEventListener sensorEventListener) {
this.parent = parent;
this.sensorType = sensorType;
this.sensorDelay = sensorDelay;
this.sensorEventListener = sensorEventListener;

init();
}

private void init() {
// Get an instance of the SensorManager
sensorManager = (SensorManager) 
parent.getSystemService(Context.SENSOR_SERVICE);

// Get an instance of the sensorType
sensor = sensorManager.getDefaultSensor( sensorType );
}

public void start() {
// Register sensors on the SensorDetection class 
sensorManager.registerListener(this.sensorEventListener, this.sensor, 
this.sensorDelay);
}

public void resume() {
start();
}

public void pause() {
// Unregister SensorDetection class
sensorManager.unregisterListener(this.sensorEventListener);
}

public void stop() {
pause(); 
}

}
*



*public class SensorPrinter {

// When open return false, you can check what happens with this two.
public boolean EXTERNAL_STORAGE_AVAILABLE = false;
public boolean EXTERNAL_STORAGE_WRITEABLE = false;

private static final int BUFFER_SIZE = 20;

private PrintWriter pw = null; 
private File file = null;
private StringBuffer text = new StringBuffer();
private int counter = 0;
private int sensorType = 0;
private String sensorName = null;


public SensorPrinter(int sensorType) {

switch( sensorType ) {
case Sensor.TYPE_ACCELEROMETER:
sensorName = "ACCELEROMETER";
break;
case Sensor.TYPE_GYROSCOPE:
sensorName = "GYROSCOPE";
break;
case Sensor.TYPE_MAGNETIC_FIELD:
sensorName = "MAGNETIC_FIELD";
break;
case Sensor.TYPE_ORIENTATION:
sensorName = "ORIENTATION";
break;
case Sensor.TYPE_LIGHT:
sensorName = "LIGHT";
break;
case Sensor.TYPE_PROXIMITY:
sensorName = "PROXIMITY";
break;
default:
sensorName = "UNKNOWN";
break; 
}

Log.i(SensorPrinter.class.getSimpleName(), "New " + sensorName + " 
sensor"); 
}

public final String getSensorName() {
return sensorName;
}

public final int getSensorType() {
return sensorType;
}

public boolean open() throws FileNotFoundException {

if( isStorageReady() ) createExternalStoragePrivateFile(); 

return isStorageReady();

}

private void createExternalStoragePrivateFile() throws 
FileNotFoundException {

file = hasExternalStoragePrivateFile();
if( file != null) pw = new PrintWriter( new FileOutputStream(file, true) );

}

private File hasExternalStoragePrivateFile() {

// Create a path where we will place our private 
// file on external storage
File root = new File(Environment.getExternalStorageDirectory(), 
Constants.MEASURES_DIRECTORY);
if ( !root.exists() ) root.mkdirs();

// Get path for the file on external storage. If external
// storage is not currently mounted this will fail.
File file = new File(root, getSensorName() + 
Constants.MEASURES_FILES_EXTENSION);

Log.i(SensorPrinter.class.getSimpleName(), "Open file: " + 
file.getAbsolutePath());

return file;

}

private boolean isStorageReady() {

String externalStorageState = Environment.getExternalStorageState();

if ( Environment.MEDIA_MOUNTED.equals( externalStorageState ) ) {
// We can read and write the media
EXTERNAL_STORAGE_AVAILABLE = EXTERNAL_STORAGE_WRITEABLE = true;
} else if ( Environment.MEDIA_MOUNTED_READ_ONLY
.equals( externalStorageState ) ) {
// We can only read the media
EXTERNAL_STORAGE_AVAILABLE = true;
EXTERNAL_STORAGE_WRITEABLE = false;
} else {
// Something else is wrong. It may be one of many other states, 
// but all we need to know is we can neither read nor write
EXTERNAL_STORAGE_AVAILABLE = EXTERNAL_STORAGE_WRITEABLE = false;
}

return (EXTERNAL_STORAGE_AVAILABLE && EXTERNAL_STORAGE_WRITEABLE);

}

public void close(boolean saving) {
if( saving ) {
pw.print(text);
pw.flush();
}

pw.close(); 
text = new StringBuffer();

if( !saving ) {
file.delete();
}

Log.i(SensorPrinter.class.getSimpleName()
, "Close, saving = " + saving);
}

public void onSensorChanged(SensorEvent event) { 

if( counter++ > BUFFER_SIZE ) {
counter = 0;
pw.print(text);
pw.flush();
text.delete(0, text.length());
}

text.append( event.timestamp + Constants.MEASURES_SEPARATOR
+ event.values[0] + Constants.MEASURES_SEPARATOR
+ event.values[1] + Constants.MEASURES_SEPARATOR 
+ event.values[2] + "\n"); 

}
*

*But i have not any data on my files, what am i do wrong?**
*
*Service runs correctly!!*
*
*

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to