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

如何解决当用户使用 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.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?