I’m trying to start a an activity only after some data is ready in the Service
I’m trying this with a timer task which constantly polls the service for the data readness
public class SplashTask extends TimerTask {
@Override
public void run() {
Log.i(MY_DEBUG_TAG, "Internet is accessible, Running some Spalsh screen Tasks ");
if(mBoundService.isDataReady()) {
Log.e(MY_DEBUG_TAG, "Data is ready in service..");
startActivityForResult(new Intent(SplashDroid.this, FunWithDataActivity.class), 3);
} else {
Log.e(MY_DEBUG_TAG, "Data not ready in service..");
}
Log.i(MY_DEBUG_TAG, "Spalsh Tasks fnished..");
}
}
Issue is that when data is ready and FunWithDataActivity about to start, i’m getting the following error
07-27 14:53:40.614: ERROR/AndroidRuntime(1042): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
You can’t use
startActivityForResultfrom non-UI thread. You can either userunOnUiThread()orHandler.post().Also, you shouldn’t really use separate thread for polling. Use
Handler‘spostDelayed()function for polling. This way you won’t wasted whole thread for simple polling. For an example see: Repeat a task with a time delay?