Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android广播接收器';我们有时不打电话_Android_Google Maps_Google Maps Api 3_Geolocation_Android Geofence - Fatal编程技术网

Android广播接收器';我们有时不打电话

Android广播接收器';我们有时不打电话,android,google-maps,google-maps-api-3,geolocation,android-geofence,Android,Google Maps,Google Maps Api 3,Geolocation,Android Geofence,我的应用程序将BroadcastReceiver与地理围栏API一起使用。当用户输入地理围栏时,将调用广播接收器 昨天,代码工作得完美无缺。BroadcastReceiver检测到我进入一个地理围栏,我通过以下代码在我的位置周围设置了一个地理围栏: builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER); geofinencesetExpirationDuration(geofiness.NEVER\u EX

我的应用程序将
BroadcastReceiver
地理围栏API一起使用。当用户输入
地理围栏时,将调用
广播接收器

昨天,代码工作得完美无缺。
BroadcastReceiver
检测到我进入一个地理围栏,我通过以下代码在我的位置周围设置了一个地理围栏:

    builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER);
geofinence
setExpirationDuration(geofiness.NEVER\u EXPIRE)
radius
1609
米(1英里)

今天,我卸载了这些应用程序,并在同一台计算机和同一台设备上再次运行了完全相同的代码。我偶然发现我的
广播接收器
接收从未被呼叫过

我检查了网络连接,没有问题。在运行
android studio
之前,我尝试在
android studio
clean
rebuild
make project
,并手动卸载
APK
,但
BroadcastReceiver
仍不工作

我发现了一个重要的观点

我在接收时放入的断点已启用(纯红色),但无效(红色带复选标记)。这就是我的意思

昨天(当完全相同的代码工作时,断点有效

然后我尝试使用
IntentService
而不是
BroadcastReceiver
(我将在稍后的问题中分享这两种服务的代码)。调用了
IntentService
,因此我的
pendingent
正在工作

之后,我再次尝试使用
BroadcastReceiver
,而
BroadcastReceiver
与昨天一样工作正常。现在,有时调用
BroadcastReceiver
,有时不调用

奇怪的部分是每次我的
BroadcastReceiver
不工作时,我需要将
pendingent
更改为
IntentService
以使我的
BroadcastReceiver
重新开始工作

一旦它重新开始工作,
BroadcastReceiver
即使半径更小(250米)
也会发出呼叫。所以我认为半径不是问题所在

我试着调试接收时的
onReceive
,发现断点必须是
有效的
(不仅仅是
启用的
),才能使我的
广播接收器工作。
换句话说,如果断点只是
启用的
(不是
有效的
)然后将永远不会调用onReceive的
onReceive

这就是我如何调用
广播接收器的方法:

    Intent intent = new Intent("mypackage.ACTION_RECEIVE_GEOFENCE");
    PendingIntent geofencePendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    LocationServices.GeofencingApi.addGeofences(
            mGoogleApiClient,
            getGeofencingRequest(),
            geofencePendingIntent
    ).setResultCallback(this);
        <service android:enabled="true" android:name="mypackage.GeofenceUpdateService" />

        <receiver android:name="mypackage.GeofenceUpdateReceiver"
            android:exported="false">
            <intent-filter >
                <action android:name="mypackage.ACTION_RECEIVE_GEOFENCE"/>
            </intent-filter>
        </receiver>
这就是我如何称呼
IntentService

        Intent intent = new Intent(this, GeofenceUpdateService.class);
        PendingIntent geofencePendingIntent = PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        LocationServices.GeofencingApi.addGeofences(
                mGoogleApiClient,
                getGeofencingRequest(),
                geofencePendingIntent
        ).setResultCallback(this);
这是我的
清单
,它包含
IntentService
BroadcastReceiver

    Intent intent = new Intent("mypackage.ACTION_RECEIVE_GEOFENCE");
    PendingIntent geofencePendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    LocationServices.GeofencingApi.addGeofences(
            mGoogleApiClient,
            getGeofencingRequest(),
            geofencePendingIntent
    ).setResultCallback(this);
        <service android:enabled="true" android:name="mypackage.GeofenceUpdateService" />

        <receiver android:name="mypackage.GeofenceUpdateReceiver"
            android:exported="false">
            <intent-filter >
                <action android:name="mypackage.ACTION_RECEIVE_GEOFENCE"/>
            </intent-filter>
        </receiver>

发布
onReceive
onHandleIntent
,因为问题不在这些代码内部,但为什么这些方法有时没有被调用

如何使我的
BroadcastReceiver
工作更稳定?(始终工作)

或者有解决办法吗?我应该改用
IntentService


非常感谢您抽出时间

查看此答案,因为您似乎不熟悉Android的工作原理


然后使用
WakefulBroadcastReceiver
而不是
BroadcastReceiver
,并检查它是否有帮助。

重新安装应用程序后,您是否手动启动了应用程序?有时候,如果你不先启动应用程序,它就不起作用了。谢谢你的回复。这些应用程序将自动运行,因为我是从
android studio
运行它的,我所说的
手动
是指在从
android studio
运行新应用程序之前卸载旧的
APK
。非常感谢您的帮助。当我测试我的
广播接收器时,我确实确保设备仍然处于唤醒状态(屏幕未处于睡眠状态)。我将尝试使用
WakefulBroadcastReceiver
并让您知道