• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps service and sensors

magmag

Lurker
Hi all,
I'm trying to detect sensor changes using services.
I have created an activity where I start the service.
The activity contains a start and a stop button to control the service.
When the service starts i want to start displaying results from the sensors. However this never happens..
Here is the code, i also have attached the project if anyone want to view it completely.

Can anyone help me? :confused:

Here is the service code
MyService.java
Code:
package com.example;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;


public class MyService extends Service {
    private static final String TAG = "MyService";
    MediaPlayer player;
    public SensorManager sensorManager;
    public Sensor sensor;
    public static float x, y, z;
    
    
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
    
    @Override
    public void onCreate() {
        Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
        Log.d(TAG, "onCreate");
    }

    @Override
    public void onDestroy() {
        Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
        Log.d(TAG, "onDestroy");
    }
    
    @Override
    public void onStart(Intent intent, int startid) {
        Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
        Log.d(TAG, "onStart");
        
        sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        sensor = sensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0);
        
    }
    public SensorEventListener accelerationListener = new SensorEventListener() {
        @Override
        public void onAccuracyChanged(Sensor sensor, int acc) {
        }
        @Override
        public void onSensorChanged(SensorEvent event) {
            x = event.values[0];
            y = event.values[1];
            z = event.values[2];
            ServicesDemo.refreshDisplay();
        }
 
    };

}
and
ServicesDemo.java
Code:
package com.example;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class ServicesDemo extends Activity implements OnClickListener {
  private static final String TAG = "ServicesDemo";
  Button buttonStart, buttonStop;
 
  private static TextView result;


  @Override
  public void onCreate(Bundle savedInstanceState) {
   
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    buttonStart = (Button) findViewById(R.id.buttonStart);
    buttonStop = (Button) findViewById(R.id.buttonStop);

    result = (TextView) findViewById(R.id.result);
    result.setText("No result yet");
    
    buttonStart.setOnClickListener(this);
    buttonStop.setOnClickListener(this);
  }
  
    public static void refreshDisplay() {
        String output = String
                .format("x is: %f / y is: %f / z is: %f", MyService.x, MyService.y, MyService.z);
        result.setText(output);
    }

  public void onClick(View src) {
    switch (src.getId()) {
    case R.id.buttonStart:
      Log.d(TAG, "onClick: starting srvice");
      startService(new Intent(this, MyService.class));
      break;
    case R.id.buttonStop:
      Log.d(TAG, "onClick: stopping srvice");
      stopService(new Intent(this, MyService.class));
      break;
    }
  }
}
 

Attachments

I do when sensor change is detected, in MyService class

Code:
public SensorEventListener accelerationListener = new SensorEventListener() {
        @Override
        public void onAccuracyChanged(Sensor sensor, int acc) {
        }
        @Override
        [B]public void onSensorChanged(SensorEvent event) {
            x = event.values[0];
            y = event.values[1];
            z = event.values[2];
            ServicesDemo.refreshDisplay();[/B]
 
Ah I see. Sorry, not sure then.

Though the way I would trouble shoot this is to log that each of these steps is happening as intended.

So make sure the log shows that the service is created
make sure the service receives the sensor events,
make sure the call is made back to the activity...
log the values at each step...

etc...
 
the problem here is that i don't know how to use the sensors in the emulator.
I'm using my phone to see if the code works, but it's like it never calls refreshDisplay

by the way,thank you for trying to help.
 
hmm, dont you need to set the sensor listener somewhere?

SensorManager | Android Developers

==================
public boolean registerListener (SensorEventListener listener, Sensor sensor, int rate)

Since: API Level 3

Registers a SensorEventListener for the given sensor.

Parameters

listener A SensorEventListener object. sensor The Sensor to register to. rate The rate sensor events are delivered at. This is only a hint to the system. Events may be received faster or slower than the specified rate. Usually events are received faster. The value must be one of SENSOR_DELAY_NORMAL, SENSOR_DELAY_UI, SENSOR_DELAY_GAME, or SENSOR_DELAY_FASTEST or, the desired delay between events in microsecond.
Returns


  • true if the sensor is supported and successfully enabled.

See Also





=====================

Also, no problem -- I enjoy helping, just ask people to try my apps if my help is useful :)
 
Back
Top Bottom