Context-detection API for Android developed as a university project
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ClassifierService.java 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5. package uk.co.md87.android.activityrecorder;
  6. import android.app.Service;
  7. import android.content.ComponentName;
  8. import android.content.Intent;
  9. import android.content.ServiceConnection;
  10. import android.os.IBinder;
  11. import android.os.RemoteException;
  12. import android.widget.Toast;
  13. import uk.co.md87.android.activityrecorder.rpc.ActivityRecorderBinder;
  14. import uk.co.md87.android.common.Classifier;
  15. /**
  16. *
  17. * @author chris
  18. */
  19. public class ClassifierService extends Service implements Runnable {
  20. ActivityRecorderBinder service = null;
  21. String classification;
  22. private ServiceConnection connection = new ServiceConnection() {
  23. public void onServiceConnected(ComponentName arg0, IBinder arg1) {
  24. service = ActivityRecorderBinder.Stub.asInterface(arg1);
  25. try {
  26. service.submitClassification(classification);
  27. } catch (RemoteException ex) {
  28. }
  29. stopSelf();
  30. }
  31. public void onServiceDisconnected(ComponentName arg0) {
  32. Toast.makeText(ClassifierService.this, R.string.error_disconnected, Toast.LENGTH_LONG);
  33. }
  34. };
  35. private float[] data;
  36. @Override
  37. public void onStart(Intent intent, int startId) {
  38. super.onStart(intent, startId);
  39. //Log.i(getClass().getName(), "Starting classifier");
  40. data = intent.getFloatArrayExtra("data");
  41. new Thread(this).start();
  42. }
  43. @Override
  44. public void onDestroy() {
  45. super.onDestroy();
  46. unbindService(connection);
  47. }
  48. @Override
  49. public IBinder onBind(Intent arg0) {
  50. return null;
  51. }
  52. public void run() {
  53. classification = new Classifier(RecorderService.model.entrySet()).classify(data);
  54. //Log.i(getClass().getName(), "Classification: " + classification);
  55. bindService(new Intent(this, RecorderService.class), connection, BIND_AUTO_CREATE);
  56. }
  57. }