如何解决当用户使用 Android Java 进入地理围栏时如何向 MainActivity 发送通知
我有一个应用程序,它在名为 SearchSchool 的活动中定义地理围栏位置。然后我在另一个类中实现了一个 broadCastReceiver 扩展,以在用户进入地理位置和离开地理位置时进行监听。除了应该向用户发送从 broadcastReceiverClass
到 SearchSchool
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 类,用于检测用户何时进入地理围栏,并应在发生时向 SearchSchool
activity 发送通知
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 举报,一经查实,本站将立刻删除。