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应用开发
转载请注明出处: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 ListView无法触发ItemClick事件
Android ListView无法触发ItemClick事件 开发中很常见的一个问题,项目中的listview不仅仅是简单的文字,常常需要自己定义listview,自己的Adapter去继承Base ...
- 彻底解决Android GPS没法定位这一顽固问题
大家去网上搜索Android定位location为null没法定位问题.预计有一大堆文章介绍怎样来解决.可是最后大家发现基本没用. 本文将从Android定位实现原理来深入分析没法定位原因并提出真正的 ...
- Android GPS应用:临近警告
前面介绍过LocationManager有一个addProximityAlert(double latitude,double longitude,float radius,long expirati ...
- android GPS定位源码 地图显示位置源码 有用到的小伙伴自提取
package com.jasgroup.cn.amhdeam; import java.io.IOException; import java.util.Iterator; import andro ...
- Android GPS 取经纬度
// 获取位置管理服务 private LocationManager locationManager;3 String mProviderName = ""; private v ...
- Android GPS定位 获取经纬度
移动 是手机与手持设备的最大特点,可以通过Eclipse的DDMS视图,模拟设备的位置变化,改变经纬度后,点击send,然后运行程序,在应用程序中,动态的获取设备位置,然后显示当前的位置信息. 获取位 ...
- Android GPS获取当前经纬度坐标
APP中可能会遇到一种需求,就是将当前所在位置的坐标传到server上,今天我提供三种途径去获取经纬度坐标信息,第一种是通过Android API来实现,另外一种通过百度地图API来实现,第三种通过天 ...
随机推荐
- html css 注释
html <!-- 注释内容 --> css注释/* 注释内容*/
- Android(Xamarin)之旅(二)
新的一年,新的开始,2016,我要做什么,大家要做什么,啦啦啦. OK,上篇已经介绍了几个简单的控件,这次,我们继续说说控件.但是可能有人认为这有什么难的,问题不在这里,而在于,如果你注意了每一个空间 ...
- 短信转发Q群
※◆☆★☆◆※欢迎使用!!!如有问题或新功能需求请联系作者QQ:82850696*4*您使用的测试版已到期,如需继续使用,请联系作者 QQ : 82850696*0*2015-1-7 23:59:59 ...
- Base64编码通过URL传值的问题
base64 编码中使用了 +号,+号通过URL传递时会变成空格,因为编码的方式的问题前台使用:Ext.encode(title_text.getValue().replace(/\+/g, '%2B ...
- [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. 答案: 内核在进行进程上下文切换时, 首 ...
- webstorm与SAE的svn仓库链接进行版本控制
这里把我使用webstorm与SAE的svn仓库链接: 1.先得设置webstorm中的版本控制,File->Settings->Version Control->Subversio ...
- 怎么定义 logger
随便打开一个 spring 的 源文件,比如 PathMatchingResourcePatternResolver.class 里面是这样定义logger 的 import org.apache. ...
- ActiveMQ学习笔记之异常
1.PUT was not successful: 404 Not Found 结果:jetty.xml中开启BlobMessage相关配置 2.Consumer消费时:FileNotFoundExc ...
- ajax异步请求Response.Redirect重定向
一个ajax异步请求报错->捕获异常->重定向错误提示页面. 一个简单的流程 结果一直搞不定.重定向无效.各种百度之. 后来突然想起 ajax的请求是不能在后台重定向的. 如果硬要重定向 ...
- [转]一个文件上传的jquery插件
http://www.jb51.net/article/51547.htm 这篇文章主要介绍了使用ajaxfileupload.js实现ajax上传文件php版,需要的朋友可以参考下 无论是P ...