java - 当用户使用 Android Java 进入地理围栏时如何向 MainActivity 发送通知
问题描述
我有一个在名为 SearchSchool 的活动中定义地理围栏位置的应用程序。然后,我在另一个类中实现了 BroadCastReceiver 扩展,以在用户输入地理位置和离开地理位置时进行监听。除了应该从 Activity 向用户发送通知的部分之外,我似乎已经正确完成了所有事情,BroadcastReceiverClass
这SearchSchool
是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, 0, intent, PendingIntent.
FLAG_UPDATE_CURRENT);
return geofencePendingIntent;
}
}
这是用于检测用户何时进入地理围栏并应SearchSchool
在发生时向活动发送通知的 BroadCastReeceiver 类
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 方法工作谢谢
解决方案
推荐阅读
- vue.js - 如何从 Firestore 的子集合中检索文档
- python - 如何创建已知距离的距离矩阵?
- ansible - 从准备收敛和验证步骤传递信息
- xamarin.forms - Xamarin 表单模板绑定到 BasePage 中的命令
- java - 如何在java中解析没有库的HTML?
- python - Is there a way to combine a function that passes object information to a url and CreateView in Django?
- c++ - 是否可以将 shared_ptr 和 weak_ptr 存储在一个容器中?
- eclipse - 在 Eclipse 项目中组织非 Maven 源代码
- c++ - 无法在 geany 上运行代码。无法执行构建命令
- java - 如何为子类创建构造函数