Why don't I get proximity alterts even though I've registered alerts?

耗尽温柔 提交于 2019-12-14 02:37:33

问题


I'm trying to simply set a proximity later for an area an for testing, I simply added this to the onCreate method of my main activity.

public void onCreate(Bundle bndBundle) {

    IntentFilter filter = new IntentFilter(WidgetService.ACTION_STOP_PROXIMITY); 
    registerReceiver(new ProximityIntentReceiver(), filter);

    LocationManager locManager = (LocationManager) this.getSystemService(LOCATION_SERVICE);
    Intent ittIntent = new Intent(this, ProximityIntentReceiver.class);
    ittIntent.putExtra(WidgetService.KEY_STOP_IDENTIFIER, 1000);
    PendingIntent pitIntent = PendingIntent.getBroadcast(this, 0, ittIntent, 0);
    locManager.addProximityAlert(60.15769, 24.94150, 150, -1, pitIntent);

    super.onCreate(bndBundle);
    getActionBar().setDisplayHomeAsUpEnabled(false);

}

..and here's the simple receiver class that I'm using

public class ProximityIntentReceiver extends BroadcastReceiver {

    private static final int NOTIFICATION_ID = 1000;

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

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

        if (entering) {
            Log.d(getClass().getSimpleName(), "entering");
        }
        else {
            Log.d(getClass().getSimpleName(), "exiting");
        }

    }

}

I'm testing this on my emulator and when I use the DDMS console to set the co-ordinates of the phone manually, I still don't see the log message.

My manifest file doesn't have any special code. I've added the correct permissions and have the code for a simple activity- no services or anything.

I read through a whole bunch of posts on StacKOverflow but I haven't been able to resolve the issue. Am I missing something in my snippet?


回答1:


You are registering this receiver dynamically, through registerReceiver(), to have it respond to broadcasts whose action string is WidgetService.ACTION_STOP_PROXIMITY.

However, the actual broadcast you are sending is trying to use an explicit Intent, identifying your receiver class. This does not line up with the IntentFilter that you are using with registerReceiver().

Either:

  • Register your receiver in the manifest and get rid of registerReceiver(), in which case your explicit Intent will work, or

  • Use new Intent(WidgetService.ACTION_STOP_PROXIMITY) instead of new Intent(this, ProximityIntentReceiver.class), so your Intent lines up with your IntentFilter

You cannot use explicit Intent objects to send broadcasts to receivers registered via registerReceiver(). An explicit Intent will only work with a manifest-registered receiver.




回答2:


make sure you type in the right coordinates. in DDMS they're reversed, longitude first, then latitude



来源:https://stackoverflow.com/questions/15586937/why-dont-i-get-proximity-alterts-even-though-ive-registered-alerts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!