临近位置点警告LocationManager
2016-07-02 23:16
323 查看
package com.gps.locationgps; import android.Manifest; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; import android.location.LocationManager; import android.os.Bundle; import android.support.v4.app.ActivityCompat; import android.support.v7.app.AppCompatActivity; /** * 临近某个位置点 */ public class ProximityActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_proximity); String locService = Context.LOCATION_SERVICE; //定位服务常量 LocationManager locationManager; locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); //定义位置点信息 杭州东站大致经纬度 double longitude = 120.189244; double latitude = 30.248885; //定义半径(5公里) float radius = 5000; // 定义 Intent Intent intent = new Intent(this, ProximityAlertReceiver.class); //将 intent 包装成 android.app.PendingIntent PendingIntent pi = PendingIntent.getBroadcast(this, -1, intent, 0); //添加临近警告 if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { // TODO: Consider calling // ActivityCompat#requestPermissions // here to request the missing permissions, and then overriding // public void onRequestPermissionsResult(int requestCode, String[] permissions, // int[] grantResults) // to handle the case where the user grants the permission. See the documentation // for ActivityCompat#requestPermissions for more details. return; } locationManager.addProximityAlert(latitude, longitude, radius, -1, pi); } }
package com.gps.locationgps; 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 { public ProximityAlertReceiver() { } @Override public void onReceive(Context context, Intent intent) { // 获取是否进入指定区域 boolean isEnter = intent.getBooleanExtra(LocationManager.KEY_PROXIMITY_ENTERING,false); if (isEnter) { //显示提示信息 Toast.makeText(context, "您已经进入北京东站", Toast.LENGTH_SHORT).show(); }else { Toast.makeText(context, "您已经离开了北京东站", Toast.LENGTH_SHORT).show(); } } }
通过DDMS的 Emulator Control面板输入经纬度,可以用来测试.
相关文章推荐
- JS option location 页面跳转实现代码
- JavaScript中的Location地址对象
- window.location不跳转的问题解决方法
- JS location几个方法小姐
- 关于js中window.location.href,location.href,parent.location.href,top.location.href的用法与区别
- JS教程:window.location使用方法的区别介绍
- javascript中的window.location.search方法简介
- IE下通过a实现location.href 获取referer的值
- JS的location.href跳出框架打开新页面的方法
- window.location 对象所包含的属性
- window.location的重写及判断location是否被重写
- html文档中的location对象属性理解及常见的用法
- 通过location.replace禁止浏览器后退防止重复提交
- js通过location.search来获取页面传来的参数
- JS中Location使用详解
- location.hash保存页面状态的技巧
- Nginx服务器中的location配置详解
- Nginx服务器的location指令匹配规则详解
- Nginx服务器中location配置的一些基本要点解析
- nginx location语法使用介绍