0

I have Preferences Activity, which start a service on preferences changes. On service I want to detect shaking action. this code I have below, works fine when I used it in a activity, but when i want to use it on service, it just wont work, this is how I used my shake detector code in service, what is wrong with my code?

public class back extends Service {
SensorManager mSensorManager;
ShakeEventListener mSensorListener;



@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public void onCreate() {
    // TODO Auto-generated method stub
    super.onCreate();

}

@Override
public void onStart(Intent intent, int startId) {
    // TODO Auto-generated method stub
    Toast.makeText(back.this, "Service Started", Toast.LENGTH_LONG).show();

     mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        mSensorListener = new ShakeEventListener();   

        mSensorListener.setOnShakeListener(new ShakeEventListener.OnShakeListener() {

          public void onShake() {
            Toast.makeText(back.this, "Shake!", Toast.LENGTH_SHORT).show();
          }
        });

}


protected void onResume() {
    mSensorManager.registerListener(mSensorListener,
        mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
        SensorManager.SENSOR_DELAY_UI);
  }

  protected void onPause() {
    mSensorManager.unregisterListener(mSensorListener);

  }

@Override
public void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
}


}

I got "Service Started" Toast which means service is started, but after I shake device I dont see "Shake!" Toast. what should I do. Thanks is advance.

Reza_Rg
  • 3,345
  • 7
  • 32
  • 48

2 Answers2

0
  1. Service not have onResume and onPause http://developer.android.com/guide/components/services.html#Lifecycle

  2. May also have problems with the display Toast from service, to verify that the code use LOG or show toast like this

    new Handler().post(new Runnable() { public void run() { Toast.makeText(getApplicationContext(), "msg", Toast.LENGTH_LONG).show(); }

avesha
  • 136
  • 1
  • 6
  • This code `Toast.makeText(back.this, "Shake!", Toast.LENGTH_SHORT).show();` will not be called, because method onResume (in which the subscription listeners to SensorManager) you never call,and he will not be called automatically so it does not Activity (see my prev post, item 1) – avesha Dec 09 '12 at 20:02
0

Remove on Resume and onPause. It is not part of service. Replace it with OnStartCommand() Please see the code.

 public int onStartCommand(Intent intent, int flags, int startId){

   mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
   mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
   mSensorManager.registerListener(mShakeDetector, mAccelerometer,    SensorManager.SENSOR_DELAY_UI,new Handler());

   return START_STICKY;
   }

That should do the trick :)

The One
  • 1,085
  • 6
  • 13
  • 25