Android GPS应用:临近警告
前面介绍过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应用:临近警告的更多相关文章
- Android GPS 临近触发
前面介绍过LocationManager有一个addProximityAlert(double latitude,double longitude,float radius,long expirati ...
- Android GPS应用开发
转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/5715879.html GPS定位系统由三部分组成,即由GPS卫星组成的空间部分,若干地面组成的控制部分和普通 ...
- android gps开发必备资料(含测试demo下载)
入门资料参考: How accurate is Android GPS? Part 1: Understanding Location Data How accurate is Android GPS ...
- 彻底解决Android GPS没法定位这一顽固问题
大家去网上搜索Android定位location为null没法定位问题.预计有一大堆文章介绍怎样来解决.可是最后大家发现基本没用. 本文将从Android定位实现原理来深入分析没法定位原因并提出真正的 ...
- android GPS定位源码 地图显示位置源码 有用到的小伙伴自提取
package com.jasgroup.cn.amhdeam; import java.io.IOException; import java.util.Iterator; import andro ...
- android gps定位LocationManager
android location provider有: * LocationManager.GPS_PROVIDER:GPS,精度比较高,但是慢而且消耗电力,而且可能因为天气原因或者障碍物而无法获取卫 ...
- Android GPS 取经纬度
// 获取位置管理服务 private LocationManager locationManager;3 String mProviderName = ""; private v ...
- Android GPS定位 获取经纬度
移动 是手机与手持设备的最大特点,可以通过Eclipse的DDMS视图,模拟设备的位置变化,改变经纬度后,点击send,然后运行程序,在应用程序中,动态的获取设备位置,然后显示当前的位置信息. 获取位 ...
- Android GPS应用:动态获取位置信息
在上文中,介绍了GPS概念及Android开发GPS应用涉及到的常用类和方法.在本文中,开发一个小应用,实时获取定位信息,包括用户所在的纬度.经度.高度.方向.移动速度等.代码如下: Activity ...
随机推荐
- SQL数据库关键字和列名冲突处理
在设计SQL数据库的时候可能由于考虑不全,使列名和数据库内关键字冲突,可能导致Query不能被正确识别,对列名要加[]处理.
- FMDB 直接将查询结果转化为字典
今天学习FMDB框架,发现非常好用的一点,就是就以把查询结果直接转化为字典 NSString *querySql = @"select * from stuInfo"; NSMut ...
- Google地图,Baidu地图数据供应商
http://janwen.iteye.com/blog/488659 Google百度 我老以为百度,Google的地图产品是自己开发的,原来是别人提供的数据, 百度的数据提供商有 北京世纪高通科 ...
- ios如何实现推送通知
推送通知的步骤:1.询问是否允许推送通知.2.如果用户允许在APPDELEGATE 中实现 - (void)application:(UIApplication *)application didRe ...
- Spring Boot简介
Spring Boot简介 Spring Boot是为了简化Spring开发而生,从Spring 3.x开始,Spring社区的发展方向就是弱化xml配置文件而加大注解的戏份.最近召开的SpringO ...
- 卡特兰数(Catalan)简介
Catalan序列是一个整数序列,其通项公式是 h(n)=C(2n,n)/(n+1) (n=0,1,2,...) 其前几项为 : 1, 1, 2, 5, 14, 42, 132, 429, 1430, ...
- 转:详细解说 STL 排序(Sort)
详细解说 STL 排序(Sort) 详细解说 STL 排序(Sort) 作者Winter 详细解说 STL 排序(Sort) 0 前言: STL,为什么你必须掌握 1 STL提供的Sort 算法 1. ...
- 转:命令和查询责任分离(CQRS)架构模式
读了“蓝皮书”距今差不多一年,它改变了我的软件开发和构建软件架构观.在我作为一名程序员期间,我尝试了许多不同的方式来构建软件.方法有很多,包括一个贫血的域模型(Anemic Domain Model) ...
- Just another Robbery(背包)
1079 - Just another Robbery PDF (English) Statistics Forum Time Limit: 4 second(s) Memory Limit: 3 ...
- zoj 3656 2-sat 不错的题
http://acm.zju.edu.cn/onlinejudge/showProblem.do? problemId=4879 TLE了一下午.然后没办法了 去搜题解 发现思路跟我的差点儿相同 可是 ...