Android 安卓接近警报未触发

Android 安卓接近警报未触发,android,broadcastreceiver,proximity,Android,Broadcastreceiver,Proximity,我正在尝试设置接近警报,下面是我的代码,但它根本没有开火 Intent intent = new Intent(ACTION_PROXIMITY_ALERT); pendingIntent = PendingIntent.getBroadcast(GMapsActivity.this, 0, intent, 0); locationManager.addProximityAlert(mNavigationDatas.get(CURRENT_INDEX).startCordinates.latit

我正在尝试设置接近警报,下面是我的代码,但它根本没有开火

Intent intent = new Intent(ACTION_PROXIMITY_ALERT);
pendingIntent = PendingIntent.getBroadcast(GMapsActivity.this, 0, intent, 0);
locationManager.addProximityAlert(mNavigationDatas.get(CURRENT_INDEX).startCordinates.latitude,
                        mNavigationDatas.get(CURRENT_INDEX).startCordinates.longitude, 100f, -1, pendingIntent);
IntentFilter filter = new IntentFilter(ACTION_PROXIMITY_ALERT); 
registerReceiver(new ProximityBroadcastListener(), filter);
权限如下

<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

我将模拟位置发送到真实设备Android v4.1.2上进行测试,模拟位置遵循正确的路径,但未调用广播侦听器

public class ProximityBroadcastListener extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {

        final String key = LocationManager.KEY_PROXIMITY_ENTERING;
        final Boolean entering = intent.getBooleanExtra(key, false);

        if (entering) {
            String instruction = mNavigationDatas.get(CURRENT_INDEX).drivingInstuctions;
            instruction = instruction.replaceAll("<[^>]*>", "");
            Log.e("", instruction);
            tts.speak(instruction, TextToSpeech.QUEUE_FLUSH, null);
            Toast.makeText(context, "entering", Toast.LENGTH_SHORT).show();
        } else {
            locationManager.removeProximityAlert(pendingIntent);
            CURRENT_INDEX++;
            locationManager.addProximityAlert(mNavigationDatas.get(CURRENT_INDEX).startCordinates.latitude,
                    mNavigationDatas.get(CURRENT_INDEX).startCordinates.longitude, 100f, -1, pendingIntent);
            Toast.makeText(context, "exiting", Toast.LENGTH_SHORT).show();
        }
    }
公共类ProximityBroadcastListener扩展了BroadcastReceiver{
@凌驾
公共void onReceive(上下文、意图){
最终字符串键=LocationManager.key\u接近度\u输入;
最终布尔输入=intent.getBooleanExtra(key,false);
如果(输入){
字符串指令=mNavigationDatas.get(当前索引).drivingInstructions;
指令=指令.replaceAll(“]*>”,“”);
Log.e(“”,指令);
tts.speak(指令,TextToSpeech.QUEUE\u FLUSH,null);
Toast.makeText(上下文,“输入”,Toast.LENGTH_SHORT).show();
}否则{
locationManager.移除代理服务器(挂起内容);
当前_索引++;
locationManager.addProximityAlert(mNavigationDatas.get(当前索引).StartCoordinates.latitude,
mNavigationDatas.get(当前索引).startCoordinates.longitude,100f,-1,PendingContent);
Toast.makeText(上下文,“退出”,Toast.LENGTH_SHORT).show();
}
}
谢谢
Saurabh

首先,我建议在添加接近警报之前注册广播接收器。如果您的设备已经在该位置,它可能会在您的接收器准备好监听之前触发意图

您可能还希望通过手动启动PendingEvent(例如通过AlarmManager、通知或只需按下按钮)来检查您的接收器是否正确注册


如果没有这样的工作,考虑使用物理设备,因为模拟器上的位置事件有一些问题。例如,传入位置的时间偏移设置为午夜,所以您的位置标准可能不匹配。< / P>在发送模拟位置时,您可以附加日志吗?我使用相同的坐标。es到我用来显示路由的模拟位置,myLocation指针移动到同一路径上,我通过另一个应用发送模拟位置。当您通过另一个应用发送模拟位置时,侦听器是否已注册,或者当该应用在后台运行时,它是否已注销?否始终已注册我使用了android mock的示例当我的应用程序进入后台时,位置和我现在不删除侦听器当你更改模拟位置时,位置管理器是否显示任何日志?