Android BroadcastReceiver 仅在使用需要 GPS 的应用程序时触发

如何解决Android BroadcastReceiver 仅在使用需要 GPS 的应用程序时触发

我正在用 Java 开发一个 Android 应用程序,允许用户登录和查看他们附近的任何已审核的摄影地点。

打开地图片段时,所有位置都从 Firestore 数据库加载。加载后,将为所有位置创建地理围栏。我有一个 onSuccessListener,它会在添加地理围栏时记录,所以我知道这是有效的。

我想要做的是让我的应用程序在用户进入地理围栏时向用户发送通知显示吐司,无论应用程序是打开还是关闭。但是,我只会在我的应用打开或其他主动使用 GPS 的应用(即 Google 地图)打开时收到这些通知

我了解模拟器中存在一个错误,这意味着要在模拟器中进行测试,您必须打开 Google 地图。正如预期的那样,这是有效的。但是,我在运行 Android 10 (SDK 29) 的真实设备上也遇到了同样的问题。

我的自定义广播接收器如下:

public class GeofencebroadcastReceiver extends broadcastReceiver {

    private static final String TAG = "GeofencebroadcastReceiver";

    @Override
    public void onReceive(Context context,Intent intent) {

        NotificationUtils notificationUtils = new NotificationUtils(context);
        GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);

        if(geofencingEvent.hasError()) {
            Log.e(TAG,"onReceive: Error receiving geofence event...");
            return;
        }

        List<Geofence> geofenceList = geofencingEvent.getTriggeringGeofences();
        for(Geofence geofence : geofenceList) {
            Log.d(TAG,"onReceive: " + geofence.getRequestId());
        }

        Location location = geofencingEvent.getTriggeringLocation();
        int transitionType = geofencingEvent.getGeofenceTransition();
        
        if(transitionType == Geofence.GEOFENCE_TRANSITION_ENTER) {
            Toast.makeText(context,"Geofence Entered...",Toast.LENGTH_SHORT).show();
            notificationUtils.sendHighPriorityNotification("Location Entered!","location has been entered",MainActivity.class);
        } else if (transitionType == Geofence.GEOFENCE_TRANSITION_DWELL) {
            Toast.makeText(context,"Geofence Dwelled...",Toast.LENGTH_SHORT).show();
            notificationUtils.sendHighPriorityNotification("Location Dwelled!","location has been dwelled",MainActivity.class);
        }
    }
}

我的 GeofenceUtils 类是:

public class GeofenceUtils extends Contextwrapper {

    private static final String TAG = "GeofenceUtils";
    PendingIntent pendingIntent;

    public GeofenceUtils(Context base) {
        super(base);
    }


    public GeofencingRequest getGeofencingRequest(Geofence geofence) {
        return new GeofencingRequest.Builder()
                .addGeofence(geofence)
                .setinitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER)
                .build();
    }


    public Geofence getGeofence(String id,LatLng latLng,float radius,int transitionTypes){
        return new Geofence.Builder()
                .setCircularRegion(latLng.latitude,latLng.longitude,radius)
                .setRequestId(id)
                .setTransitionTypes(transitionTypes)
                .setLoiteringDelay(0)
                .setExpirationDuration(Geofence.NEVER_EXPIRE)
                .build();
    }


    public PendingIntent getPendingIntent() {
        if(this.pendingIntent != null) {
            return this.pendingIntent;
        }
        Intent intent = new Intent(this,GeofencebroadcastReceiver.class);
        pendingIntent = PendingIntent.getbroadcast(this,1010,intent,PendingIntent.FLAG_UPDATE_CURRENT);
        return pendingIntent;
    }


    public String getErrorString(Exception e)
    {
        if (e instanceof ApiException) {
            ApiException apiException = (ApiException) e;
            switch (apiException.getStatusCode()){
                case GeofenceStatusCodes
                        .GEOFENCE_NOT_AVAILABLE:
                    return "GEOFENCE_NOT_AVAILABLE";
                case GeofenceStatusCodes
                        .GEOFENCE_TOO_MANY_GEOFENCES:
                    return "GEOFENCE_TOO_MANY_GEOFENCES";
                case GeofenceStatusCodes
                        .GEOFENCE_TOO_MANY_PENDING_INTENTS:
                    return "GEOFENCE_TOO_MANY_PENDING_INTENTS";
            }
        }
        return e.getLocalizedMessage();
    }
}

通过阅读类似的问题,我确定使用 PendingIntent.getbroadcast 而不是 PendingIntent.getService,但我仍然没有任何运气。

任何帮助将不胜感激!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?