sensors in service problem

Put your problem here if it does not fit any of the other categories.

sensors in service problem

Postby magmag » Fri Mar 04, 2011 1:32 pm

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.

Here is the service code
MyService.java
Code: Select all
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: Select all
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;
    }
  }
}



Can anyone help me? :(
Attachments
ServicesDemo.zip
(31.77 KiB) Downloaded 17 times
magmag
Once Poster
Once Poster
 
Posts: 1
Joined: Fri Mar 04, 2011 1:26 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 17 guests