0

嗨,我必须创建一个应用程序,用户可以在其中标记位置,以便有一个与该位置相对应的警报。我从广播接收器开始,我向广播接收器注册,一切正常,但对于一个位置,待定意图一次又一次地触发相同本文中描述的问题

在这里

并且有很多疑问很多人都面临着同样的问题

第一篇文章中有一个解决方案,当我在 activty onCreate 方法中注册广播接收器时,它仅在我按下回退然后自动注册取消注册时该活动处于焦点时才起作用。我希望广播接收器一直工作。实际上,当我单击保存时会出现一个警报,然后为该位置保存一个接近警报这里是用于注册与我的应用程序相关的接近警报的相关代码

String s=System.currentTimeMillis()+"";


Intent intent = new Intent(PROX_ALERT_INTENT+s);
intent.putExtra("longitude", longitude);
intent.putExtra("latitude", latitude);

       PendingIntent proximityIntent = PendingIntent.getBroadcast(getApplicationContext(), i, intent, PendingIntent.FLAG_UPDATE_CURRENT);



locationManager.addProximityAlert(

           latitude, // the latitude of the central point of the alert region

           longitude, // the longitude of the central point of the alert region

           1000, // the radius of the central point of the alert region, in meters

           -1, // time for this proximity alert, in milliseconds, or -1 to indicate no expiration

           proximityIntent // will be used to generate an Intent to fire when entry to or exit from the alert region is detected

      );

IntentFilter filter = new IntentFilter(PROX_ALERT_INTENT+s); 

     registerReceiver(new ProximityAlertReceiver(), filter);

我正在使用包含常量和时间戳的操作字符串创建意图。然后我正在注册具有相同常量 + 时间戳的广播接收器。警报工作正常,但仅在活动处于前台时才有效,否则警报将不起作用。任何人都可以告诉如何添加接近警报,以便它只会触发一次或任何替代解决方案来完成此任务。

4

1 回答 1

0

因为 ProximityAlert 触发意图的次数更多,并且我在带有注释的 Activty 中显示警报,所以我使用了 Activity 的 onNewIntent 方法,因此生成的应用程序实例不超过一个。

于 2012-12-21T13:28:01.960 回答