>
> I had test code inside code sample. The cleaning Service is:
public class SensorPrinterService extends Service implements
SensorEventListener {
private Controller controller = null;
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();
controller = Controller.getInstance();
controller.init( this.getApplicationContext() );
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_ACCELEROMETER: "
+ 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;
}
}
}
--
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