前面介绍过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. 使用jQuery.form插件,实现完美的表单异步提交

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs ...

  2. c# (nop中)下拉列表(有外键)

    第一种情况.view视图加载出来时就有值,实现步骤如下 1.在操作的界面Model中建立public List<SelectListItem> xxx(取名){ get; set; } 2 ...

  3. NSArray其中的方法--遍历,

    1. ForLoop, For - in, enumerateObjects这个三个方法的区别: 遍历一个数组用For-in最快. 通过Value查询index的时候, 面对大量的数组推荐使用 enu ...

  4. js 图片预览

    图片预览 $('#pac_recipe').change(function() { var imgsrc = ''; ]) { //chrome firefox imgsrc = window.URL ...

  5. Zabbix配置文件详解之服务端zabbix_server

    zabbix作为运维邻域不可缺少的一员,它的各种文档可是数不胜数啊,但是关于配置文件的解释与说明就有点少.这里列出zabbix配置文件篇之zabbix_server. Zabbix Server端配置 ...

  6. Unresolved external 'LresultFromObject'

    [ILINK32 Error] Error: Unresolved external 'LresultFromObject' referenced from D:\PROGRAM FILES\EMBA ...

  7. 第38讲:List伴生对象操作方法代码实战

    今天来看一下List伴生对像的操作方法 让我们来看下代码  println(List.apply(1,2,3))//等同于List(1,2,3)     println(List.range(1, 4 ...

  8. c# 后台弹出对话框 和前台弹出新页面

    前台: window.showModalDialog("daoru.aspx", "NPOI_daoru_window", "status:false ...

  9. java基础-jdbc——三种方式加载驱动建立连接

    String url = "jdbc:mysql://localhost:3306/student?Unicode=true&characterEncoding=utf-8" ...

  10. (译)cocos2d-x跨android&ios平台开发入门教程

    免责申明(必读!):本博客提供的所有教程的翻译原稿均来自于互联网,仅供学习交流之用,切勿进行商业传播.同时,转载时不要移除本申明.如产生任何纠纷,均与本博客所有人.发表该翻译稿之人无任何关系.谢谢合作 ...