2

I want to create an application where my sensors get values in another thread while the main thread is still working. I tried with just another thread, but it didn't working. I tried with a service and a IntentService, but the sensors start after the main thread. My current code is this.

MyService.java

public class MyService extends IntentService implements SensorEventListener 
{
    public MyService()
    {
        super("MyService");
    }

    Sensor mLight;
    SensorManager mSensorManager;
    float max;
    float currentLux;
    float currentProx;

    @Override
    public IBinder onBind(Intent intent) 
    {
        Log.d("Service", "On bind");
        return null;
    }

    @Override
    public void onCreate() 
    {
        Log.d("Service", "On create");
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) 
    {
        // TODO Auto-generated method stub

    }

    @Override
    public void onSensorChanged(SensorEvent event) 
    {
        if( event.sensor.getType() == Sensor.TYPE_LIGHT)
        {
            currentLux=(int) event.values[0];
            float perc=(float) ((currentLux*100)/max);
            Log.d("Light", String.valueOf(perc));
        }   
        else if(event.sensor.getType() == Sensor.TYPE_PROXIMITY)
        {
            currentProx=(int) event.values[0];
            Log.d("Proximity", String.valueOf(currentProx));
        }
    }

    @Override
    public void onDestroy() 
    {
        Toast.makeText(this, "Service Stopped", Toast.LENGTH_LONG).show();
        Log.d("Service", "On stop");
        mSensorManager.unregisterListener(this);
    }

    @Override
    public void onStart(Intent intent, int startid) 
    {
        Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
        Log.d("Service", "On start");
    }

    @Override//here u should register sensor and write onStartCommand not onStart
    public int onStartCommand(Intent intent, int flags, int startId) 
    {
        mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
        mSensorManager.registerListener(this, mSensorManager
                .getDefaultSensor(Sensor.TYPE_LIGHT),
                SensorManager.SENSOR_DELAY_FASTEST);
        mLight = mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
        mSensorManager.registerListener(this, mSensorManager
                .getDefaultSensor(Sensor.TYPE_PROXIMITY),
                SensorManager.SENSOR_DELAY_FASTEST);
        max=mLight.getMaximumRange();
        Log.d("MAX=", String.valueOf(max));
        return Service.START_STICKY;
    }

    @Override
    protected void onHandleIntent(Intent intent) 
    {
    }

}

Listeners.java

public class Listeners extends Activity implements OnClickListener 
{
     Button button0;
     Intent listenersIntent;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listeners);
        lista= new ArrayList<Float>();
        button0 = (Button) findViewById(R.id.button0);
        button0.setOnClickListener(this);
        listenersIntent = new Intent(this, MyService.class);
        startService(listenersIntent);
        for (int i=0; i<999; i++)
            Log.d("Main", String.valueOf(i));
    }

    @Override
    public void onClick(View arg0) 
    {
        stopService(listenersIntent);
    }
}

My problem is that i get always 1-999 before any proximity/light values, while i want them to run together. Can you help me please?

As As
  • 2,049
  • 4
  • 17
  • 33
  • why do you extend intentservice if you are going to override onstart and oncreate in it ? – njzk2 Oct 01 '13 at 13:44
  • I removed these 2 methods, but i get always 1-999 before the sensors start – As As Oct 01 '13 at 14:50
  • can't you post the log? – njzk2 Oct 01 '13 at 14:51
  • 10-01 16:56:04.973: D/Main(29247): 0 10-01 16:56:04.973: D/Main(29247): 1 ... 10-01 16:56:05.093: D/Main(29247): 998 10-01 16:56:05.153: D/MAX=(29247): 102000.0 10-01 16:56:06.993: D/Light(29247): 0.0 10-01 16:56:06.993: D/Proximity(29247): 0.0 10-01 16:56:07.313: D/Light(29247): 1.5686275 10-01 16:56:07.313: D/Proximity(29247): 1.0 10-01 16:56:07.418: D/Light(29247): 0.0 10-01 16:56:07.418: D/Proximity(29247): 0.0 10-01 16:56:11.133: D/Service(29247): On stop – As As Oct 01 '13 at 14:57

1 Answers1

-1

You start a service from within an onCreate method. That method, as the service initialization, run on the main thread. It means the service cannot run its initialization at the same time as your onCreate is running (not to mention that logging 0-998 it very very fast - 80ms from your log).

Moreover, you are registering listeners. Those will be called even after your service started.

You cannot synchronize those 2 actions like that. (And there is not enough details as to why you need any synchronization at all to point you in the right direction)

njzk2
  • 38,969
  • 7
  • 69
  • 107
  • I wanted to use this code in another application and run it in background from the start, so i tried to use that for to see if my listeners worked on background, but even if i use a thread or a service i can't get it work. – As As Oct 01 '13 at 15:29
  • your listeners work in the background, and you are supposed to return from onCreate as fast as possible as it runs on the main thread. Now, I don't understand your question. – njzk2 Oct 01 '13 at 15:33
  • I thought it too, but i use the listener class in the other application (gps listener and these listeners), but only the gps was working, not returning any light/proximity values. – As As Oct 01 '13 at 15:41