首页 > 解决方案 > Android中的地理围栏服务

问题描述

我正在开发一个与地理围栏相关的android应用程序,我尝试广播接收器在进入地理围栏时获取通知,我只会在应用程序运行时工作,然后我尝试服务来获取通知,但现在进入时没有通知地理围栏。

这是代码

**

package com.example.fypautosilenceapp;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
import androidx.annotation.Nullable;
import com.google.android.gms.location.Geofence;
import com.google.android.gms.location.GeofencingEvent;
import java.util.List;
public class GeofenceService extends Service {
    private static final String TAG = "GeofenceBroadcastRecive";
    AudioManager audioManager;

    
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        NotificationHelper notificationHelper=new NotificationHelper(getApplicationContext());
        GeofencingEvent geofencingEvent=GeofencingEvent.fromIntent(intent);
        if(geofencingEvent.hasError()){
            Toast.makeText(getApplicationContext(), "Geofence has error", Toast.LENGTH_SHORT).show();
            Log.d(TAG,"OnRecive:Error recive geofence event...");
        }
        List<Geofence> geofenceslist=geofencingEvent.getTriggeringGeofences();
        //Location geofencelocation=geofencingEvent.getTriggeringLocation();
        for (Geofence geofence:geofenceslist){
            Log.d(TAG,"OnRecive:"+geofence.getRequestId());
        }
        int transtiontype=geofencingEvent.getGeofenceTransition();
        switch (transtiontype){
            case Geofence.GEOFENCE_TRANSITION_ENTER:
             //   Toast.makeText(getApplicationContext(),"Enter geofence",Toast.LENGTH_SHORT).show();
                notificationHelper.sendHighPriorityNotification("Enter geofence","",MapsActivity.class);
              //  audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
                break;
            case Geofence.GEOFENCE_TRANSITION_EXIT:
             //   Toast.makeText(getApplicationContext(),"Exit geofence",Toast.LENGTH_SHORT).show();
                notificationHelper.sendHighPriorityNotification("Exit geofence","",MapsActivity.class);
               // audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
                break;
            case Geofence.GEOFENCE_TRANSITION_DWELL:
            //    Toast.makeText(getApplicationContext(),"Enter Dwell",Toast.LENGTH_SHORT).show();
                notificationHelper.sendHighPriorityNotification("Enter Dewell","",MapsActivity.class);
           //     audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
                break;
        }
        return null;
    }
}

**

标签: javaandroidgeofencingandroid-geofencegeofence

解决方案


推荐阅读