android - GEOFENCE TRANSITION DWELL 不适用于 android

标签 android android-geofence

我正在处理 geofence,我遇到的问题与 GEOFENCE TRANSITION DWELL 有关,而不是 TriggerEnter and Exit Transition工作正常。我花了很多时间在谷歌上搜索,但发现了太多关于我的问题的信息。我只想每隔一分钟向用户发送一次 通知 如果用户留在 geofence radius 内。

这是我的地理围栏助手类:

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();

}
}

这是我的地理围栏服务类:

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);
}

最佳答案

您需要设置停留的初始触发器以及输入。
添加

GeofencingRequest.INITIAL_TRIGGER_DWELL

在您的 geoFencingRequest 中。

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

注意:Geofence.GEOFENCE_TRANSITION_DWELL 仅在您进入地理围栏并停留在您在 setLoiteringDelay(timedelay) 方法中设置的时间内时调用,在您进入地理围栏 60 秒后将触发驻留。

关于android - GEOFENCE TRANSITION DWELL 不适用于 android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41356043/

相关文章:

java - 从android中的电话簿获取联系人

java - 如何使用android中的服务组件制作倒计时器?

android - 应用程序被杀死时地理围栏不起作用

android - 权限异常,即使在 list 中设置了权限

java - 尝试模拟 java.lang.System 时出现 MockitoException

android - Android 中的自定义蓝牙协议(protocol)支持

android - 蓝牙适配器禁用并重新启用后如何让 Android 连接?

Android Google Geofencing 过渡跳跃,即使设备不动

android - 无法在 Android 中添加地理围栏,因为 "network location is disabled"

Android 11 用户无法授予后台位置权限?