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来实现,第三种通过天 ...
随机推荐
- 使用jQuery.form插件,实现完美的表单异步提交
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs ...
- c# (nop中)下拉列表(有外键)
第一种情况.view视图加载出来时就有值,实现步骤如下 1.在操作的界面Model中建立public List<SelectListItem> xxx(取名){ get; set; } 2 ...
- NSArray其中的方法--遍历,
1. ForLoop, For - in, enumerateObjects这个三个方法的区别: 遍历一个数组用For-in最快. 通过Value查询index的时候, 面对大量的数组推荐使用 enu ...
- js 图片预览
图片预览 $('#pac_recipe').change(function() { var imgsrc = ''; ]) { //chrome firefox imgsrc = window.URL ...
- Zabbix配置文件详解之服务端zabbix_server
zabbix作为运维邻域不可缺少的一员,它的各种文档可是数不胜数啊,但是关于配置文件的解释与说明就有点少.这里列出zabbix配置文件篇之zabbix_server. Zabbix Server端配置 ...
- Unresolved external 'LresultFromObject'
[ILINK32 Error] Error: Unresolved external 'LresultFromObject' referenced from D:\PROGRAM FILES\EMBA ...
- 第38讲:List伴生对象操作方法代码实战
今天来看一下List伴生对像的操作方法 让我们来看下代码 println(List.apply(1,2,3))//等同于List(1,2,3) println(List.range(1, 4 ...
- c# 后台弹出对话框 和前台弹出新页面
前台: window.showModalDialog("daoru.aspx", "NPOI_daoru_window", "status:false ...
- java基础-jdbc——三种方式加载驱动建立连接
String url = "jdbc:mysql://localhost:3306/student?Unicode=true&characterEncoding=utf-8" ...
- (译)cocos2d-x跨android&ios平台开发入门教程
免责申明(必读!):本博客提供的所有教程的翻译原稿均来自于互联网,仅供学习交流之用,切勿进行商业传播.同时,转载时不要移除本申明.如产生任何纠纷,均与本博客所有人.发表该翻译稿之人无任何关系.谢谢合作 ...