前面介绍过LocationManager有一个addProximityAlert(double latitude,double longitude,float radius,long expiration,PendingIntent intent)方法,该方法可用于添加临近警告。其参数说明如下:

latitude:指定固定点的经度。

longitude:指定固定点的纬度。

radius:指定半径长度。

expiration:指定经过多少毫秒后该临近警告就会过期失效。-1表示永不过期。

intent:该参数指定临近该固定点时触发该intent对应的组件.

下面这个小程序可以检测手机是否进入和离开了指定点的指定范围内。代码如下:

Activity:

  1. package com.home.proximityalert;
  2. import com.home.receiver.ProximityAlertReceiver;
  3. import android.app.Activity;
  4. import android.app.PendingIntent;
  5. import android.content.Context;
  6. import android.content.Intent;
  7. import android.location.LocationManager;
  8. import android.os.Bundle;
  9. public class ProximityAlertTestActivity extends Activity {
  10. @Override
  11. protected void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.main);
  14. // 获取系统LocationManager服务
  15. LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
  16. // 定义成都市成华区的大致经度、纬度
  17. double longitude = 104.10;
  18. double latitude = 30.67;
  19. // 定义半径(5公里)
  20. float radius = 5000;
  21. // 定义Intent
  22. Intent intent = new Intent(this, ProximityAlertReceiver.class);
  23. // 将Intent包装成PendingIntent对象
  24. PendingIntent pi = PendingIntent.getBroadcast(this, -1, intent, 0);
  25. // 添加临近警告
  26. locationManager.addProximityAlert(latitude, longitude, radius, -1, pi);
  27. }
  28. }

BroadcastReceiver(ProximityAlertReceiver):

  1. package com.home.receiver;
  2. import android.content.BroadcastReceiver;
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.location.LocationManager;
  6. import android.widget.Toast;
  7. public class ProximityAlertReceiver extends BroadcastReceiver {
  8. @Override
  9. public void onReceive(Context context, Intent intent) {
  10. // 获取是否进入指定区域
  11. boolean isEnter = intent.getBooleanExtra(
  12. LocationManager.KEY_PROXIMITY_ENTERING, false);
  13. if (isEnter) {
  14. // 给出提示信息
  15. Toast.makeText(context, "您已经进入成都市成华区", Toast.LENGTH_LONG).show();
  16. } else {
  17. // 给出提示信息
  18. Toast.makeText(context, "您已经离开成都市成华区", Toast.LENGTH_LONG).show();
  19. }
  20. }
  21. }

权限:

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

Android GPS 临近触发的更多相关文章

  1. Android GPS应用开发

    转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/5715879.html GPS定位系统由三部分组成,即由GPS卫星组成的空间部分,若干地面组成的控制部分和普通 ...

  2. android gps开发必备资料(含测试demo下载)

    入门资料参考: How accurate is Android GPS? Part 1: Understanding Location Data How accurate is Android GPS ...

  3. Android ListView无法触发ItemClick事件

    Android ListView无法触发ItemClick事件 开发中很常见的一个问题,项目中的listview不仅仅是简单的文字,常常需要自己定义listview,自己的Adapter去继承Base ...

  4. 彻底解决Android GPS没法定位这一顽固问题

    大家去网上搜索Android定位location为null没法定位问题.预计有一大堆文章介绍怎样来解决.可是最后大家发现基本没用. 本文将从Android定位实现原理来深入分析没法定位原因并提出真正的 ...

  5. Android GPS应用:临近警告

    前面介绍过LocationManager有一个addProximityAlert(double latitude,double longitude,float radius,long expirati ...

  6. android GPS定位源码 地图显示位置源码 有用到的小伙伴自提取

    package com.jasgroup.cn.amhdeam; import java.io.IOException; import java.util.Iterator; import andro ...

  7. Android GPS 取经纬度

    // 获取位置管理服务 private LocationManager locationManager;3 String mProviderName = ""; private v ...

  8. Android GPS定位 获取经纬度

    移动 是手机与手持设备的最大特点,可以通过Eclipse的DDMS视图,模拟设备的位置变化,改变经纬度后,点击send,然后运行程序,在应用程序中,动态的获取设备位置,然后显示当前的位置信息. 获取位 ...

  9. Android GPS获取当前经纬度坐标

    APP中可能会遇到一种需求,就是将当前所在位置的坐标传到server上,今天我提供三种途径去获取经纬度坐标信息,第一种是通过Android API来实现,另外一种通过百度地图API来实现,第三种通过天 ...

随机推荐

  1. html css 注释

    html <!--       注释内容 --> css注释/*   注释内容*/

  2. Android(Xamarin)之旅(二)

    新的一年,新的开始,2016,我要做什么,大家要做什么,啦啦啦. OK,上篇已经介绍了几个简单的控件,这次,我们继续说说控件.但是可能有人认为这有什么难的,问题不在这里,而在于,如果你注意了每一个空间 ...

  3. 短信转发Q群

    ※◆☆★☆◆※欢迎使用!!!如有问题或新功能需求请联系作者QQ:82850696*4*您使用的测试版已到期,如需继续使用,请联系作者 QQ : 82850696*0*2015-1-7 23:59:59 ...

  4. Base64编码通过URL传值的问题

    base64 编码中使用了 +号,+号通过URL传递时会变成空格,因为编码的方式的问题前台使用:Ext.encode(title_text.getValue().replace(/\+/g, '%2B ...

  5. [Chapter 3 Process]Practice 3.9 Describe the actions token by a kernel to content-switch between processes.

    3.9 Describe the actions token by a kernel to content-switch between processes. 答案: 内核在进行进程上下文切换时, 首 ...

  6. webstorm与SAE的svn仓库链接进行版本控制

    这里把我使用webstorm与SAE的svn仓库链接: 1.先得设置webstorm中的版本控制,File->Settings->Version Control->Subversio ...

  7. 怎么定义 logger

    随便打开一个 spring 的 源文件,比如  PathMatchingResourcePatternResolver.class 里面是这样定义logger 的 import org.apache. ...

  8. ActiveMQ学习笔记之异常

    1.PUT was not successful: 404 Not Found 结果:jetty.xml中开启BlobMessage相关配置 2.Consumer消费时:FileNotFoundExc ...

  9. ajax异步请求Response.Redirect重定向

    一个ajax异步请求报错->捕获异常->重定向错误提示页面.  一个简单的流程 结果一直搞不定.重定向无效.各种百度之. 后来突然想起 ajax的请求是不能在后台重定向的. 如果硬要重定向 ...

  10. [转]一个文件上传的jquery插件

    http://www.jb51.net/article/51547.htm 这篇文章主要介绍了使用ajaxfileupload.js实现ajax上传文件php版,需要的朋友可以参考下     无论是P ...