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

latitude:指定固定点的经度。

longitude:指定固定点的纬度。

radius:指定半径长度。

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

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

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

Activity:

package com.home.proximityalert;

import com.home.receiver.ProximityAlertReceiver;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.location.LocationManager;
import android.os.Bundle; public class ProximityAlertTestActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 获取系统LocationManager服务
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// 定义成都市成华区的大致经度、纬度
double longitude = 104.10;
double latitude = 30.67;
// 定义半径(5公里)
float radius = 5000;
// 定义Intent
Intent intent = new Intent(this, ProximityAlertReceiver.class);
// 将Intent包装成PendingIntent对象
PendingIntent pi = PendingIntent.getBroadcast(this, -1, intent, 0);
// 添加临近警告
locationManager.addProximityAlert(latitude, longitude, radius, -1, pi);
} }

BroadcastReceiver(ProximityAlertReceiver):

package com.home.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.location.LocationManager;
import android.widget.Toast; public class ProximityAlertReceiver extends BroadcastReceiver { @Override
public void onReceive(Context context, Intent intent) {
// 获取是否进入指定区域
boolean isEnter = intent.getBooleanExtra(
LocationManager.KEY_PROXIMITY_ENTERING, false);
if (isEnter) {
// 给出提示信息
Toast.makeText(context, "您已经进入成都市成华区", Toast.LENGTH_LONG).show();
} else {
// 给出提示信息
Toast.makeText(context, "您已经离开成都市成华区", Toast.LENGTH_LONG).show();
}
} }

权限:

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

Android GPS应用:临近警告的更多相关文章

  1. Android GPS 临近触发

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

  2. Android GPS应用开发

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

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

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

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

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

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

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

  6. android gps定位LocationManager

    android location provider有: * LocationManager.GPS_PROVIDER:GPS,精度比较高,但是慢而且消耗电力,而且可能因为天气原因或者障碍物而无法获取卫 ...

  7. Android GPS 取经纬度

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

  8. Android GPS定位 获取经纬度

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

  9. Android GPS应用:动态获取位置信息

    在上文中,介绍了GPS概念及Android开发GPS应用涉及到的常用类和方法.在本文中,开发一个小应用,实时获取定位信息,包括用户所在的纬度.经度.高度.方向.移动速度等.代码如下: Activity ...

随机推荐

  1. Hadoop学习笔记(4)hadoop集群模式安装

    具体的过程参见伪分布模式的安装,集群模式的安装和伪分布模式的安装基本一样,只有细微的差别,写在下面: 修改masers和slavers文件: 在hadoop/conf文件夹中的配置文件中有两个文件ma ...

  2. openGL 旋转的图形 矩阵操作

    #include <windows.h> #ifdef __APPLE__ #include <GLUT/glut.h> #else #include <GL/glut. ...

  3. 一周学会Mootools 1.4中文教程:序论

    刚才发了几篇Mootools(以后直接简称Moo或Mt,看到这两个名字的时候不要感到奇怪),有一位热心的朋友"追杀"告诉我说现在已经出到1.4了,就不要再纠结于1.2了,想象一下有 ...

  4. php分页实例附代码

    一个典型的PHP分页实例代码分享,学习php的朋友肯定用得到,主要是了解思路: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transit ...

  5. ural1057 Amount of degrees 位数统计

    #include <iostream> #include <string> using namespace std; ][]; void init(){ f[][] =; ;i ...

  6. [置顶] java 枚举

    1. 什么是枚举?枚举就是用来存放一组固定的常量. 2. 枚举有什么作用?一些程序在运行时,它需要的数据不能是任意的,而必须是一定范围内的值:例如性别  男和女. public enum Gender ...

  7. kinect for windows - 手势识别之一,C++实现

    用C++来实现手势识别是比较困难的,所以在这个例子,我们只实现了握拳和松手的手势识别,其他没有实现. 先上个效果图: 在这个程序里,我们打开了kinect的RGB流,深度流,骨骼数据流和手势识别流.其 ...

  8. 搞不清FastCgi与PHP-fpm之间是个什么样的关系 - SegmentFault

    搞不清FastCgi与PHP-fpm之间是个什么样的关系 - SegmentFault 搞不清FastCgi与PHP-fpm之间是个什么样的关系 3赞 踩 收藏 我在网上查fastcgi与php-fp ...

  9. JS动态增加页面上的控件实例

    <input   type="button"   value="继续添加"   onclick="append();"/ > & ...

  10. 使用jetty和mongodb实现简易网盘接口

    依赖库: 1,jetty(提供http方式接口) 2,mongodb的java驱动(访问mongodb存取文件) 3,thumbnailator包,进行缩略图生成 4,commons-fileuplo ...