如何在用户使用Android Java进入地理围栏时向MainActivity发送通知

9 浏览
0 Comments

如何在用户使用Android Java进入地理围栏时向MainActivity发送通知

我有一个应用程序,在名为SearchSchool的Activity中定义了一个Geofence位置。然后,在另一个类中实现了一个BroadCastReceiver扩展,用于监听用户进入和离开地理位置。除了从BroadcastReceiverClass到SearchSchool Activity(它是AppCompatActivity类的扩展)发送通知的部分之外,我似乎已经做得很正确了。\n以下是在SearchSchool活动中定义Geofence构建器的代码:\n

public class Search_School extends AppCompatActivity {
  PendingIntent geofencePendingIntent;
    private GeofencingClient geofencingClient;
    List geofences = new ArrayList<>();
    //replace with real latitude and longitude of your geofence
     //define the longitude and latitude of the geofence and radius
    private double lat = 37.8136;
    double lon = 74.0060;
    private float radius = 100;
    private long EXPIRATION_TIME = 10000;
     @Override
     protected void OnCreate(Bundle savedInstanceState){
    geofencingClient = LocationServices.getGeofencingClient(this);
        geofences.add(new Geofence.Builder()
                .setRequestId("MyGeofence")
                .setCircularRegion(lat, lon, radius)
                .setExpirationDuration(EXPIRATION_TIME).
                        setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_EXIT)
                .build());
        //add a geofence to the client
       geofencingClient.addGeofences(getGeofencingRequest(), getGeofencePendingIntent())
                .addOnSuccessListener(new OnSuccessListener() {
                    @Override
                    public void onSuccess(Void aVoid) {
                        Toast.makeText(getActivity(), "Geofence added with success",Toast.LENGTH_LONG).show();
                    }
                }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                 Toast.makeText(getActivity(),"Geofence addtion failed",Toast.LENGTH_LONG).show();
            }
        });
        }
    //create a  geofence request
   private GeofencingRequest getGeofencingRequest() {
        GeofencingRequest.Builder builder =new GeofencingRequest.Builder();
        builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER);
        builder.addGeofences(geofences);
        return builder.build();
    }
     //Create a geofence pendingRequest
           private PendingIntent getGeofencePendingIntent() {
            // Reuse the PendingIntent if we already have it.
            if (geofencePendingIntent != null) {
                return geofencePendingIntent;
            }
            Intent intent = new Intent(this, GeofenceBroadcastReceiver.class);
            // We use FLAG_UPDATE_CURRENT so that we get the same pending intent back when
            // calling addGeofences() and removeGeofences().
            geofencePendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.
                    FLAG_UPDATE_CURRENT);
            return geofencePendingIntent;
        }
}

\n以下是用于检测用户何时进入Geofence并在发生时向SearchSchool活动发送通知的BroadCastReeceiver类:\n

package com.example.fastuniforms;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
import com.google.android.gms.location.Geofence;
import com.google.android.gms.location.GeofenceStatusCodes;
import com.google.android.gms.location.GeofencingEvent;
import java.util.List;
import static android.content.ContentValues.TAG;
public class GeofenceBroadcastReceiver extends BroadcastReceiver {
    // ...
    public void onReceive(Context context, Intent intent) {
        GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);
        if (geofencingEvent.hasError()) {
            String errorMessage = GeofenceStatusCodes
                    .getStatusCodeString(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 triggeringGeofences = geofencingEvent.getTriggeringGeofences();
            // Get the transition details as a String.
            String geofenceTransitionDetails = getGeofenceTransitionDetails(
                    this,
                    geofenceTransition,
                    triggeringGeofences
            );
            // I havea problem creating a notification method in a non  Activity class
            sendNotification(geofenceTransitionDetails);
            Log.i(TAG, geofenceTransitionDetails);
        } else {
            // Log the error.
            Log.e(TAG,"An error occurred, try again in a little bit");
        }
    }
      //i have a major problem here getting the details and sending them
    private String getGeofenceTransitionDetails(GeofenceBroadcastReceiver geofenceBroadcastReceiver, int geofenceTransition, List triggeringGeofences) {
    }
}

\n我在第二个类中突出显示的两个区域有问题,请帮助我使getGeofenceTransitionDetails和sendNotification方法正常工作,谢谢。

0