You can use both Service
and IntentService
. Here I have given one of the method which should work for you. For difference between Service
and IntentService
follow this link.
public class service extends Service implements SensorEventListener {
@Override
public void onCreate() {
super.onCreate();
//register your sensor manager listener here
}
@Override
public void onDestroy() {
//unregister your listener here
}
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
//detect the shake and do your work here
}
}