Updating the sample to use the Main thread for showing a Toast message
Bug: 19358654
Change-Id: I1be5fe5a92a6ea8d7a3afe92e8a83af1aca3de76
diff --git a/wearable/wear/Geofencing/Application/src/main/java/com/example/android/wearable/geofencing/GeofenceTransitionsIntentService.java b/wearable/wear/Geofencing/Application/src/main/java/com/example/android/wearable/geofencing/GeofenceTransitionsIntentService.java
index 8ae0cbc..53117e3 100644
--- a/wearable/wear/Geofencing/Application/src/main/java/com/example/android/wearable/geofencing/GeofenceTransitionsIntentService.java
+++ b/wearable/wear/Geofencing/Application/src/main/java/com/example/android/wearable/geofencing/GeofenceTransitionsIntentService.java
@@ -23,8 +23,11 @@
import static com.example.android.wearable.geofencing.Constants.TAG;
import android.app.IntentService;
+import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
+import android.os.Handler;
+import android.os.Looper;
import android.util.Log;
import android.widget.Toast;
@@ -100,13 +103,25 @@
// Delete the data item when leaving a geofence region.
mGoogleApiClient.blockingConnect(CONNECTION_TIME_OUT_MS, TimeUnit.MILLISECONDS);
Wearable.DataApi.deleteDataItems(mGoogleApiClient, GEOFENCE_DATA_ITEM_URI).await();
- Toast.makeText(this, getString(R.string.exiting_geofence),
- Toast.LENGTH_SHORT).show();
+ showToast(this, R.string.exiting_geofence);
mGoogleApiClient.disconnect();
}
}
}
+ /**
+ * Showing a toast message, using the Main thread
+ */
+ private void showToast(final Context context, final int resourceId) {
+ Handler mainThread = new Handler(Looper.getMainLooper());
+ mainThread.post(new Runnable() {
+ @Override
+ public void run() {
+ Toast.makeText(context, context.getString(resourceId), Toast.LENGTH_SHORT).show();
+ }
+ });
+ }
+
@Override
public void onConnected(Bundle connectionHint) {
}