如何在有人走进地理围栏时显示Toast消息?

如何在有人走进地理围栏时显示Toast消息?

问题描述:

所以我有这个geofence设置。当某人进入或离开地理围栏时,我可以显示通知,但我似乎无法为此留言。我想你会把Toast消息放在onHandleIntent()方法中,但这似乎不起作用。我希望你们能提供一些见解。如何在有人走进地理围栏时显示Toast消息?

这是我的课。

public class GeofenceTransitionsIntentService extends IntentService { 
    protected static final String TAG = "geofence-transitions-service"; 

    public GeofenceTransitionsIntentService() { 
     // Use the TAG to name the worker thread. 
     super(TAG); 
    } 

    @Override 
    public void onCreate() 
    { 
     super.onCreate(); 
    } 

    @Override 
    protected void onHandleIntent(Intent intent) { 
     GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent); 
     if (geofencingEvent.hasError()) { 
      String errorMessage = GeofenceErrorMessages.getErrorString(this, 
        geofencingEvent.getErrorCode()); 
      Log.e(TAG, errorMessage); 
      return; 
     } 

     // Get the transition type. 
     int geofenceTransition = geofencingEvent.getGeofenceTransition(); 

     // Test that the reported transition was of interest. 
     if (geofenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER || 
       geofenceTransition == Geofence.GEOFENCE_TRANSITION_EXIT) { 

      // Get the geofences that were triggered. A single event can trigger multiple geofences. 
      List<Geofence> triggeringGeofences = geofencingEvent.getTriggeringGeofences(); 

      // Get the transition details as a String. 
      String geofenceTransitionDetails = getGeofenceTransitionDetails(
        this, 
        geofenceTransition, 
        triggeringGeofences 
      ); 

      // Send notification and log the transition details. 
      sendNotification(geofenceTransitionDetails); 
      Toast.makeText(getBaseContext(),"Entered my geoFence :D",Toast.LENGTH_SHORT).show(); 
      Log.i(TAG, geofenceTransitionDetails); 
     } else { 
      // Log the error. 
      Log.e(TAG, getString(R.string.geofence_transition_invalid_type, geofenceTransition)); 
     } 
    } 

    private String getGeofenceTransitionDetails(
      Context context, 
      int geofenceTransition, 
      List<Geofence> triggeringGeofences) { 

     String geofenceTransitionString = getTransitionString(geofenceTransition); 

     // Get the Ids of each geofence that was triggered. 
     ArrayList triggeringGeofencesIdsList = new ArrayList(); 
     for (Geofence geofence : triggeringGeofences) { 
      triggeringGeofencesIdsList.add(geofence.getRequestId()); 
     } 
     String triggeringGeofencesIdsString = TextUtils.join(", ", triggeringGeofencesIdsList); 

     return geofenceTransitionString + ": " + triggeringGeofencesIdsString; 
    } 

    private String getTransitionString(int transitionType) { 
     switch (transitionType) { 
      case Geofence.GEOFENCE_TRANSITION_ENTER: 
       return getString(R.string.geofence_transition_entered); 
      case Geofence.GEOFENCE_TRANSITION_EXIT: 
       return getString(R.string.geofence_transition_exited); 
      default: 
       return getString(R.string.unknown_geofence_transition); 
     } 
    } 

    private void sendNotification(String notificationDetails) { 
     // Create an explicit content Intent that starts the main Activity. 
     Intent notificationIntent = new Intent(getApplicationContext(), Main.class); 

     // Construct a task stack. 
     TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); 

     // Add the main Activity to the task stack as the parent. 
     stackBuilder.addParentStack(Main.class); 

     // Push the content Intent onto the stack. 
     stackBuilder.addNextIntent(notificationIntent); 

     // Get a PendingIntent containing the entire back stack. 
     PendingIntent notificationPendingIntent = 
       stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); 

     // Get a notification builder that's compatible with platform versions >= 4 
     NotificationCompat.Builder builder = new NotificationCompat.Builder(this); 

     // Define the notification settings. 
     builder.setSmallIcon(R.drawable.ic_launcher) 
       // In a real app, you may want to use a library like Volley 
       // to decode the Bitmap. 
       .setLargeIcon(BitmapFactory.decodeResource(getResources(), 
         R.drawable.ic_launcher)) 
       .setColor(Color.RED) 
       .setContentTitle(notificationDetails) 
       .setContentText(getString(R.string.geofence_transition_notification_text)) 
       .setContentIntent(notificationPendingIntent); 

     // Dismiss notification once the user touches it. 
     builder.setAutoCancel(true); 

     // Get an instance of the Notification manager 
     NotificationManager mNotificationManager = 
       (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 

     // Issue the notification 
     mNotificationManager.notify(0, builder.build()); 
    } 

} 

目前,我在onHandleIntent()方法底部的if语句中放置了toast。我的完整源代码直接来自Google的geofence教程:http://io2015codelabs.appspot.com/codelabs/geofences#1

这是一个IntentService,因此在后台线程中执行对onHandleIntent的回调。

您需要在主UI线程上执行Toast才能在屏幕上正确显示它。

这个答案看一看,看看如何从服务进入主线程

Accessing UI thread handler from a service

+0

啊我不知道这是一个单独的线程。感谢您的建议虐待尝试 – TheQ

试试这个:

@Override 
protected void onHandleIntent(Intent intent) { 

    GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent); 
    if (geofencingEvent.hasError()) { 
     String errorMessage = GeofenceErrorMessages.getErrorString(this, 
       geofencingEvent.getErrorCode()); 
     Log.e(TAG, errorMessage); 
     return; 
    } 

    int geofenceTransition = geofencingEvent.getGeofenceTransition(); 


    if (geofenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER || 
      geofenceTransition == Geofence.GEOFENCE_TRANSITION_EXIT) { 

     // Get the geofences that were triggered. A single event can trigger multiple geofences. 
     List<Geofence> triggeringGeofences = geofencingEvent.getTriggeringGeofences(); 


     String geofenceTransitionDetails = getGeofenceTransitionDetails(
       this, 
       geofenceTransition, 
       triggeringGeofences 
     ); 

     // Showing toast for location transition 
     Handler handler = new Handler(Looper.getMainLooper()); 
     handler.post(new Runnable() { 

      @Override 
      public void run() { 
       Toast.makeText(context,geofenceTransitionDetails,Toast.LENGTH_SHORT).show(); 
      } 
     }); 


     Log.i(TAG, geofenceTransitionDetails); 
    } else { 
     // Log the error. 
     Log.e(TAG, getString(R.string.geofence_transition_invalid_type, geofenceTransition)); 
    } 
}