收到接近警报意图时如何接收位置信息?

发布于 2024-10-24 01:49:33 字数 174 浏览 3 评论 0原文

我需要使用 LocationManager 中的 addProximityAlert 方法添加多个接近警报。问题是,当我收到通知用户接近我正在侦听警报的区域之一的意图时,如果用户正在读取 KEY_PROXIMITY_ENTERING 的区域进入或退出,我可以获得,但我不知道该区域是什么他正在进入或退出。有什么办法可以获取此类信息吗?

I need to add more than one proximity alert with addProximityAlert method from LocationManager. The problem is that when i receive the intent informing that the user is near to one of the regions that i am listening for alerts i can get if the user is entering or exiting in a region reading KEY_PROXIMITY_ENTERING, but i dont know what is the region he is entering or exiting. Is there any way the get this kind of info?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我最亲爱的 2024-10-31 01:49:33

好吧,要添加 ProximityAlert,您需要一个 PendingIntent,可以使用 PendingIntent.getBroadcast(aContext, aRequestCode, anIntent, 0); 获取 PeindingIntent;现在,在第三个参数“anIntent”中,您可以放置​​任何您想要的数据,例如位置对象,通过位置获取的城市名称,

我这样做:

private static final float DEFAULT_PROXIMITY = 1000f;
int requestCode = 0;
for (Location p : locationList) {
                Double _lat = p.getLatitude();
                Double _lon = p.getLongitude();
                float radious = DEFAULT_PROXIMITY;
                long expiration = -1;

                Intent intent = new Intent(PROXIMITY_ALERT_ACTION_FIRED);
                intent.putExtra( "location-lat" , p.getLatitude());
                intent.putExtra( "location-lon" , p.getLongitude());
                PendingIntent proximityIntent = PendingIntent.getBroadcast(this, requestCode, intent, 0);
                locationManager.addProximityAlert(_lat, _lon, radious, expiration, proximityIntent);
                requestCode++;
            }

现在,当您收到广播消息时,您必须获取位置来自传递的意图的信息,类似这样;

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

  Boolean entering = intent.getBooleanExtra(key, false);
  if(entering){
    long lat = intent.getLongExtra("location-lat", -1);
    long lon = intent.getLongExtra("location-lon", -1);
    log.info("", "entering: " + lat + lon);
  }else{
    long lat = intent.getLongExtra("location-lat", -1);
    long lon = intent.getLongExtra("location-lon", -1);
    log.info("", "exiting: " + lat + lon);

  }
}

干杯

well, to add a ProximityAlert you need a PendingIntent, a PeindingIntent can be obtained with PendingIntent.getBroadcast(aContext, aRequestCode, anIntent, 0); Now, in the third parameter 'anIntent' you can put any data you want, like a Location object, a city name obtained with the location,

i do this like this:

private static final float DEFAULT_PROXIMITY = 1000f;
int requestCode = 0;
for (Location p : locationList) {
                Double _lat = p.getLatitude();
                Double _lon = p.getLongitude();
                float radious = DEFAULT_PROXIMITY;
                long expiration = -1;

                Intent intent = new Intent(PROXIMITY_ALERT_ACTION_FIRED);
                intent.putExtra( "location-lat" , p.getLatitude());
                intent.putExtra( "location-lon" , p.getLongitude());
                PendingIntent proximityIntent = PendingIntent.getBroadcast(this, requestCode, intent, 0);
                locationManager.addProximityAlert(_lat, _lon, radious, expiration, proximityIntent);
                requestCode++;
            }

now, when you receive the broadcast message, you must obtain the location info from the passed intent, something like this;

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

  Boolean entering = intent.getBooleanExtra(key, false);
  if(entering){
    long lat = intent.getLongExtra("location-lat", -1);
    long lon = intent.getLongExtra("location-lon", -1);
    log.info("", "entering: " + lat + lon);
  }else{
    long lat = intent.getLongExtra("location-lat", -1);
    long lon = intent.getLongExtra("location-lon", -1);
    log.info("", "exiting: " + lat + lon);

  }
}

cheers

忘羡 2024-10-31 01:49:33

为您感兴趣的每个区域定义多个操作

Define multiple Actions for every region you are interested in

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文