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

问题描述

我有一个应用程序,它在名为 SearchSchool 的活动中定义地理围栏位置。然后我在另一个类中实现了一个 broadCastReceiver 扩展,以在用户进入地理位置和离开地理位置时进行监听。除了应该向用户发送从 broadcastReceiverClassSearchSchool Activity 的通知(它是 AppCompatActivity 类的扩展)之外,我似乎已经正确完成了所有操作。 这是 SearchSchool 活动中定义 Geofence 构建器...

代码
public class Search_School extends AppCompatActivity {

  PendingIntent geofencePendingIntent;
    private GeofencingClient geofencingClient;
    List<Geofence> 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<Void>() {
                    @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,intent,PendingIntent.
                    FLAG_UPDATE_CURRENT);
            return geofencePendingIntent;
        }

}

这是 broadCastReeceiver 类,用于检测用户何时进入地理围栏,并应在发生时向 SearchSchoolactivity 发送通知

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<Geofence> 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<Geofence> triggeringGeofences) {

    }
}


我对第二节课中突出显示的两个区域有疑问,请帮助我使 getGeofenceTransitionDetails 和 ``sendNotification` 方法起作用,谢谢

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...