I'm currently trying to show a toast from IntentService, if a device detects an accelerometer. To do so, I searched and learned that I can implement an Handler. However, it is not quite working. The code compiles and runs on an emulator without any error, but the toast doesn't show. I was wondering if I could get some help to spot mistakes in my code. The code is shown below.
Any help would be appreciated!
public class AccelService extends IntentService implements SensorEventListener{
private SensorManager mySensorManager;
private Handler toastHandler;
public AccelService(){
super("AccelerometerIntentService");
}
...
private class ToastRunnable implements Runnable{
String toastText;
public ToastRunnable(String text){
toastText = text;
}
@Override
public void run(){
Toast.makeText(getApplicationContext(), toastText, Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onHandleIntent(Intent intent){
toastHandler = new Handler();
initialize();
}
public void initialize(){
mySensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
if(mySensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER) != null){
toastHandler.post(new ToastRunnable("Accelerometer Detected!"));
}
}
...
}