After geofence transition how do i do something on main Activity?

青春壹個敷衍的年華 提交于 2020-01-01 06:40:07

问题


When someone enters one of my geofences I want to make a layout visible over my map and start playing some audio. I have been following some tutorials and I know how to do push notifications when they enter, but this all happens in another IntentService class.

Maybe this is what onResult is for? Im not sure how to get geofence info from the result parameter

LocationServices.GeofencingApi.addGeofences(
            gApiClient,
            getGeofencingRequest(),
            getGeofencePendingIntent()
    ).setResultCallback(this);

 @Override
public void onResult(Result result) {


}

heres my intentService class

public class GeofenceTransitionsIntentService extends IntentService {
private final String TAG = "Geofence_transitions";


public GeofenceTransitionsIntentService() {
    super("geo-service");
}


@Override
protected void onHandleIntent(Intent intent) {
    GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);
    if (geofencingEvent.hasError()) {
        String errorMessage = 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_DWELL ||
            geofenceTransition == Geofence.GEOFENCE_TRANSITION_EXIT) {

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

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

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
                        .setSmallIcon(R.drawable.ic_media_play)
                        .setContentTitle("Geofence!")
                        .setContentText(geofenceTransitionDetails);

        mBuilder.setStyle(new NotificationCompat.BigTextStyle(mBuilder).bigText(geofenceTransitionDetails).setBigContentTitle("Big title")
                .setSummaryText(geofenceTransitionDetails));


        // Send notification and log the transition details.
        int mNotificationId = 001;

        NotificationManager mNotifyMgr =
                (NotificationManager)    getSystemService(NOTIFICATION_SERVICE);

        mNotifyMgr.notify(mNotificationId, mBuilder.build());

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

    }

}

How do people usually get the info of a triggered geofence and do something on the main thread?


回答1:


Use LocalBroadcastManager to send a broadcast to any Context that has to listen to the events. Here's an example of what you should do in your Activity:

public class MyActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceBundle);
        //Your code here

        LocalBroadcastManager lbc = LocalBroadcastManager.getInstance(this);
        GoogleReceiver receiver = new GoogleReceiver(this);
        lbc.registerReceiver(googleReceiver, new IntentFilter("googlegeofence"));
        //Anything with this intent will be sent to this receiver

    }

    static class GoogleReceiver extends BroadcastReceiver{

        MyActivity mActivity;

        public GoogleReceiver(Activity activity){
            mActivity = (MyActivity)activity;
        }

        @Override
        public void onReceive(Context context, Intent intent) {
            //Handle the intent here
        }
    }
}

And here is what you should put down for your intentservice:

public class GeofenceTransitionsIntentService extends IntentService {

//Any stuff you need to do here    

@Override
protected void onHandleIntent(Intent intent) {
    GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);

    //Collect all the stuff you want to send to activity here


    Intent lbcIntent = new Intent("googlegeofence"); //Send to any reciever listening for this

    lbcIntent.putExtra("Key", Value);  //Put whatever it is you want the activity to handle

    LocalBroadcastManager.getInstance(this).sendBroadcast(lbcIntent);  //Send the intent
}


来源:https://stackoverflow.com/questions/34384101/after-geofence-transition-how-do-i-do-something-on-main-activity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!