接近警报接收器不会触发

时间:2012-02-15 20:12:15

标签: android proximity

我正在为动态创建的叠加创建接近警报,但我遇到的问题是,当用户在半径范围内移动时,广播接收器不会触发。

private void registerIntents() {
    for(int i = 0; i < points.size(); i++) {
        double intentLat, intentLon;
        intentLat = (int)(points.get(i).getLat() * 1E6);
        intentLon = (int)(points.get(i).getLon() * 1E6);
        setProximityAlert(intentLat, intentLon, i+1,i);
    }
}
private void setProximityAlert(double lat, double lon, final long eventID, int requestCode)
{
    // 100 meter radius
    float radius = 100f;

    // Expiration is 10 Minutes (10mins * 60secs * 1000milliSecs)
    long expiration = 600000;

    LocationManager locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    int uniqueID = requestCode;
    String intentAction = PROXIMITY_INTENT_ACTION + uniqueID;

    //Intent intent = new Intent(PROXIMITY_INTENT_ACTION);

    //intent.putExtra(ProximityAlertReceiver.EVENT_ID_INTENT_EXTRA, eventID);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), uniqueID, new Intent(intentAction), PendingIntent.FLAG_CANCEL_CURRENT);
    mIntentFilter = new IntentFilter(intentAction);
    locManager.addProximityAlert(lat, lon, radius, expiration, pendingIntent);
}

这是用于创建接近警报的代码,以下代码用于注册广播接收器。我在on create方法中有它。

        this.registerIntents();
    registerReceiver(new ProximityAlertReceiver(), mIntentFilter);

我真的很感激任何帮助,因为我已经工作了几天没有成功。

这些是我在清单文件中的权限。

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

1 个答案:

答案 0 :(得分:0)

尝试使用直接进入您班级的PendingIntent:

    Intent i = new Intent(this, MyActivity.class);
    PendingIntent pi = PendingIntent.getActivity(getApplicationContext(), 0, i, 0);     

这跳过了通过BroadcastReceiver并简化代码的步骤。如果你还没有看到通知,那么我会检查以确保LocationManager正在生成任何位置更新。