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

前端 未结 1 2030
[愿得一人]
[愿得一人] 2021-02-11 01:55

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

相关标签:
1条回答
  • 2021-02-11 02:19

    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 googleReceiver = 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
    }
    
    0 讨论(0)
提交回复
热议问题