GEOFENCE过渡驻留在android中不起作用

GEOFENCE过渡驻留在android中不起作用,android,android-geofence,Android,Android Geofence,我正在处理地理围栏,我遇到了与地理围栏过渡暂停未触发相关的问题,而进入和退出过渡工作正常。我花了很多时间在谷歌上搜索,但发现我的问题太多了。如果用户停留在地理围栏半径范围内,我只想在每一分钟后向用户发送通知 这是我的Geofence助手类: public class GeofenceHelper { private Context mContext; private GeofencingRequest geoFencingRequest; private Intent intent; priv

我正在处理
地理围栏
,我遇到了与
地理围栏过渡暂停
触发
相关的问题,而
进入和退出过渡
工作正常。我花了很多时间在谷歌上搜索,但发现我的问题太多了。如果用户停留在
地理围栏半径范围内
,我只想在每一分钟后向用户发送
通知

这是我的Geofence助手类:

public class GeofenceHelper {

private Context mContext;
private GeofencingRequest geoFencingRequest;
private Intent intent;
private PendingIntent pendintIntent;

public GeofenceHelper(Context mContext) {
    this.mContext = mContext;
}

public void addGeofence(List<ExcelDataModel> excelDataModel, ResultCallback<Status> statusResultCallback) {
    List<Geofence> geofenceList = new ArrayList<>();
    for (ExcelDataModel dataModel : excelDataModel) {
        if (!TextUtils.isEmpty(dataModel.getLatitude()) && !TextUtils.isEmpty(dataModel.getLongitude()) && !TextUtils.isEmpty(dataModel.getRadius())) {
            Geofence geofence = new Geofence.Builder()
                    .setRequestId(String.valueOf(dataModel.getId()))
                    .setCircularRegion(Double.parseDouble(dataModel.getLatitude()), Double.parseDouble(dataModel.getLongitude()), Float.parseFloat(dataModel.getRadius()))
                    .setExpirationDuration(Geofence.NEVER_EXPIRE)
                    .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_EXIT | Geofence.GEOFENCE_TRANSITION_DWELL)
                    .setLoiteringDelay(60000)
                    .build();

            geofenceList.add(geofence);
        }
    }

    if (geofenceList.size() < 1){
        Toast.makeText(mContext, "NO valid geofence.", Toast.LENGTH_SHORT).show();
        return;
    }

    geoFencingRequest = new GeofencingRequest.Builder()
            .setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER)
            .addGeofences(geofenceList)
            .build();


    intent = new Intent(mContext, GeofenceService.class);
    intent.putExtra(GeofenceService.EXTRA_ID, 12);
    pendintIntent = PendingIntent.getService(mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        return;
    }
    LocationServices.GeofencingApi
            .addGeofences(App.getGoogleApiHelper().googleApiClient, geoFencingRequest, pendintIntent)
            .setResultCallback(statusResultCallback);

    Toast.makeText(mContext, "Total Geofences: " + geofenceList.size(), Toast.LENGTH_SHORT).show();

}
}
公共类GeofenceHelper{
私有上下文;
私人GeofensingRequest GeofensingRequest;
私人意图;
私人挂件挂件;
公共GeofenceHelper(上下文mContext){
this.mContext=mContext;
}
public void addGeofence(列出excelDataModel、ResultCallback状态ResultCallback){
List geofenceList=新建ArrayList();
用于(ExcelDataModel数据模型:ExcelDataModel){
如果(!TextUtils.isEmpty(dataModel.getLatitude())和&!TextUtils.isEmpty(dataModel.getLatitude())和&!TextUtils.isEmpty(dataModel.getRadius()){
Geofence Geofence=新建Geofence.Builder()
.setRequestId(String.valueOf(dataModel.getId()))
.setCircularRegion(Double.parseDouble(dataModel.getLatitude()),Double.parseDouble(dataModel.getLatitude()),Float.parseFloat(dataModel.getRadius())
.setExpirationDuration(地理围栏永不过期)
.setTransitionTypes(geofeen.geofeen_TRANSITION_进入| geofeen.geofeen_TRANSITION_退出| geofeen.geofeen_TRANSITION_驻留)
.setLoiteringDelay(60000)
.build();
地理围栏列表。添加(地理围栏);
}
}
if(geofenceList.size()<1){
Toast.makeText(mContext,“无有效地理围栏”,Toast.LENGTH_SHORT.show();
返回;
}
geoFencingRequest=新建geoFencingRequest.Builder()
.setInitialTrigger(GeofencingRequest.INITIAL\u TRIGGER\u ENTER)
.addGeofences(geofenceList)
.build();
intent=新的intent(mContext,geofenservice.class);
意向。putExtra(GeofenceService.EXTRA_ID,12);
pendintent=pendingent.getService(mContext,0,intent,pendingent.FLAG_UPDATE_CURRENT);
if(ActivityCompat.checkSelfPermission(mContext、Manifest.permission.ACCESS\u FINE\u LOCATION)!=PackageManager.permission\u已授予){
返回;
}
LocationServices.GeofencingApi
.addGeofences(App.getGoogleAppHelper().GoogleAppClient、geoFencingRequest、Pendintent)
.setResultCallback(statusResultCallback);
Toast.makeText(mContext,“总地理围栏数:+geofengineslist.size(),Toast.LENGTH\u SHORT.show();
}
}
这是我的Geofence服务课程:

public class GeofenceService extends IntentService {

public static final String TAG = GeofenceService.class.getSimpleName();
public static String EXTRA_ID = "extra_id";


@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    return super.onStartCommand(intent, flags, startId);
}

public GeofenceService() {
    super(TAG);
}

@Override
protected void onHandleIntent(Intent intent) {
    GeofencingEvent event = GeofencingEvent.fromIntent(intent);
    if (event.hasError()) {
        Toast.makeText(this, "Goefence Error: " + event.getErrorCode(), Toast.LENGTH_SHORT).show();
    } else {
        int transition = event.getGeofenceTransition();
        Log.d(TAG, "onHandleIntent: " + transition);
        List<Geofence> geofences = event.getTriggeringGeofences();

        Geofence geofence = geofences.get(0);
        String requestId = geofence.getRequestId();

        ExcelDataModel excelData = App.getDatabaseAdapter().getRecordById(requestId);
        CategoryModel categoryModel = App.getDatabaseAdapter().getCategoryById(excelData.getCategory());

        if (transition == Geofence.GEOFENCE_TRANSITION_ENTER) {

            if (categoryModel.getStatus() == 1){

                sendNotification(excelData.getName(),excelData.getNotificationMessage());
            }
            App.getDatabaseAdapter().addOrUpdateNOtificaion(new NotificationModel(requestId, excelData.getName(), excelData.getNotificationMessage(), getDateTime()));

        }else if (transition == Geofence.GEOFENCE_TRANSITION_EXIT){

            if (categoryModel.getStatus() == 1) {
                sendNotification(excelData.getName(), "Exit Geofence");
            }

            App.getDatabaseAdapter().addOrUpdateNOtificaion(new NotificationModel(requestId, excelData.getName(), excelData.getNotificationMessage(), getDateTime()));
        } else if (transition == Geofence.GEOFENCE_TRANSITION_DWELL) {
            Log.d(TAG, "DWELL geofence- " + requestId);
            if(categoryModel.getStatus() == 1) {
                sendNotification(excelData.getName(), "Hello buddy You are still here!!");
            }

            App.getDatabaseAdapter().addOrUpdateNOtificaion(new NotificationModel(requestId, excelData.getName(), excelData.getNotificationMessage(), getDateTime()));

        }
    }
}

public void sendNotification(String title, String mess) {

    Intent ii = new Intent(this, MainActivity.class);
    ii.putExtra("check",R.id.notification_id);
    PendingIntent pi = PendingIntent.getActivity(this, 0, ii, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder ncompat = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_notifications_active_black_24dp)
            .setContentTitle(title)
            .setContentText(mess)
            .setContentIntent(pi)
            .setDefaults(Notification.DEFAULT_VIBRATE)
            .setAutoCancel(true);
    Notification notification = ncompat.build();
    NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    nm.notify(0, notification);
}
公共类GeofenceService扩展了IntentService{
public static final String TAG=geofenservice.class.getSimpleName();
公共静态字符串EXTRA\u ID=“EXTRA\u ID”;
@凌驾
公共int onStartCommand(Intent Intent、int标志、int startId){
返回super.onStartCommand(intent、flags、startId);
}
公共地理信息服务(){
超级(标签);
}
@凌驾
受保护的手部内容无效(意图){
GeofencingEvent事件=GeofencingEvent.fromIntent(intent);
if(event.hasError()){
Toast.makeText(这是“Goefence错误:+event.getErrorCode(),Toast.LENGTH_SHORT).show();
}否则{
int transition=event.getGeofenceTransition();
Log.d(标签“onHandleIntent:+转换”);
List geofenses=event.getTriggeringGeofenses();
Geofence Geofence=geofences.get(0);
String requestId=geofines.getRequestId();
ExcelDataModel excelData=App.getDatabaseAdapter().getRecordById(requestId);
CategoryModel CategoryModel=App.getDatabaseAdapter().getCategoryById(excelData.getCategory());
if(transition==地理围栏。地理围栏\u transition\u ENTER){
如果(categoryModel.getStatus()==1){
sendNotification(excelData.getName(),excelData.getNotificationMessage());
}
App.getDatabaseAdapter().addorUpdateNotification(新的NotificationModel(requestId,excelData.getName(),excelData.getNotificationMessage(),getDateTime());
}else if(transition==地理围栏。地理围栏\u transition\u EXIT){
如果(categoryModel.getStatus()==1){
sendNotification(excelData.getName(),“退出地理围栏”);
}
App.getDatabaseAdapter().addorUpdateNotification(新的NotificationModel(requestId,excelData.getName(),excelData.getNotificationMessage(),getDateTime());
}else if(过渡==地理围栏。地理围栏\过渡\驻留){
Log.d(标签“驻留地理围栏-”+请求ID);
如果(categoryModel.getStatus()==1){
sendNotification(excelData.getName(),“你好,伙计,你还在这里!!”;
}
App.getDatabaseAdapter().addorUpdateNotification(新的NotificationModel(requestId,excelData.getName(),excelData.getNotificationMessage(),getDateTime());
}
}
}
公共void sendNotification(字符串标题、字符串混乱){
意图ii=新意图(此,MainActivity.class);
ii.额外支付(“支票”,R.id.notification_-id);
PendingEvent pi=PendingEvent.getActivity(此,0,ii,PendingEvent.FLAG_更新_当前);
NotificationCompat.Builder ncompat=新建NotificationCompat.Builder(此)
.setSmallIcon(R.drawable.ic\u通知\u活动\u黑色\u 24dp)
.setContentTitle(标题)
.setContentText(mess)
.setContentIntent(pi)
.setDefaults(通知.DEFAULT\u振动)
.setAutoCancel(真);
Notification Notification=ncompat.build();
NotificationManager nm=(NotificationManager)getSystemService(通知服务);
nm.notify(0,notification);
}

您需要在输入的同时设置驻留的初始触发器。
添加

GeofencingRequest.INITIAL_触发器_驻留

在你的地理探索中

    geoFencingRequest = new GeofencingRequest.Builder()
        .setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER|GeofencingRequest.INITIAL_TRIGGER_DWELL)
        .addGeofences(geofenceList)
        .build();
注:土工围栏
// Test that the reported transition was of interest.
if (geofenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER ||
    geofenceTransition == Geofence.GEOFENCE_TRANSITION_EXIT ||
    geofenceTransition == Geofence.GEOFENCE_TRANSITION_DWELL ) {
    ...
geofenceList.add(new Geofence.Builder()
        .setRequestId("geofence1")
        .setCircularRegion(
                latitude, //lat
                longitude, //lon
                radius // radius
        )
        .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_DWELL)
        .setLoiteringDelay(10000) //10 seconds for staying in geofence
        .setExpirationDuration(Geofence.NEVER_EXPIRE)
        .build());