LocationListener监听位置变化,当进入到某一距离内时发出提醒
来源:互联网 发布:购物中心数据分析 编辑:程序博客网 时间:2024/04/25 14:23
项目中需要这样的要求:
启动一个服务一直在背后监听当前位置变化,如果进入到离某个地点n千米内,发出一个Notification提醒用户附近有什么什么......
这里我采用的策略是这样的:
首先监听网络,如果联网了就启动距离监听服务,否则关闭距离监听服务。因为网络一旦断了,何谈距离变化?
其次,是否需要开机自启动网络监听,这样也就等于启动了距离监听服务。
其三,一旦进入到某个范围之内,就马上关闭距离监听服务,否则会不停的提醒,用户会觉得很烦。
基于以上步骤,首先实现一个距离监听服务
网络监听服务,其实注册了一个广播,监听手机CONNECTIVITY_ACTION动作即可
如有必要加上开机启动
以上service,BroadcastReceiver都需要注册,再加上网络权限等
启动一个服务一直在背后监听当前位置变化,如果进入到离某个地点n千米内,发出一个Notification提醒用户附近有什么什么......
这里我采用的策略是这样的:
首先监听网络,如果联网了就启动距离监听服务,否则关闭距离监听服务。因为网络一旦断了,何谈距离变化?
其次,是否需要开机自启动网络监听,这样也就等于启动了距离监听服务。
其三,一旦进入到某个范围之内,就马上关闭距离监听服务,否则会不停的提醒,用户会觉得很烦。
基于以上步骤,首先实现一个距离监听服务
package com.mobovip.app;import java.text.DecimalFormat;import java.util.ArrayList;import android.app.Notification;import android.app.NotificationManager;import android.app.PendingIntent;import android.app.Service;import android.content.Context;import android.content.Intent;import android.location.Location;import android.location.LocationListener;import android.location.LocationManager;import android.os.Bundle;import android.os.IBinder;import android.util.Log;public class LocationService extends Service{private Context context;public static ArrayList<Store> stores=new ArrayList<Store>();public static double distance = 3;//7430kmprivate LocationManager locationManager;private NotificationManager notificationManager;@Overridepublic IBinder onBind(Intent intent) {// TODO Auto-generated method stubreturn null;}@Overridepublic void onCreate() {super.onCreate();context = this;locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);//if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){//locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000, 1f, locationListener);//}else{locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,1000, 1f,locationListener);//}//Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); // if(location != null){// checkDistance(location);// } }private final LocationListener locationListener = new LocationListener() {//当坐标改变时触发此函数,如果Provider传进相同的坐标,它就不会被触发 public void onLocationChanged(Location location) { // log it when the location changes if (location != null) { checkDistance(location); } } // Provider被disable时触发此函数,比如GPS被关闭 public void onProviderDisabled(String provider) { } // Provider被enable时触发此函数,比如GPS被打开 public void onProviderEnabled(String provider) { } // Provider的在可用、暂时不可用和无服务三个状态直接切换时触发此函数 public void onStatusChanged(String provider, int status, Bundle extras) { }};@Overridepublic void onDestroy() {super.onDestroy();if(locationManager!=null){locationManager.removeUpdates(locationListener); locationManager=null;}}private void checkDistance(Location location) {if (location != null) {float[] results = new float[1];for (Store store : stores) {Location.distanceBetween(location.getLatitude(),location.getLongitude(), store.getLatitude(),store.getLongitude(), results);float result=(results[0] / 1000);//kmif (result < distance) {showNotification(store);stopSelf();//不要频繁的提醒break;}}}}private static final int NOTIFY_ID = 0;private void showNotification(Store store) {notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);Intent intent = new Intent(this, BGR.class);//intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK);intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,intent, PendingIntent.FLAG_UPDATE_CURRENT);// FLAG_ONE_SHOTNotification notification = new Notification.Builder(context).setTicker(context.getString(R.string.app_name, "")).setContentTitle(store.getStoreName()+"("+store.getDistance()+")").setContentText(store.getStoreAddress()).setContentIntent(pendingIntent).setSmallIcon(R.drawable.ic_launch_notify).setAutoCancel(true).setWhen(System.currentTimeMillis()).setDefaults(Notification.DEFAULT_ALL).getNotification();notificationManager.notify(NOTIFY_ID, notification);}}
网络监听服务,其实注册了一个广播,监听手机CONNECTIVITY_ACTION动作即可
package com.mobovip.app;import android.app.Service;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.content.IntentFilter;import android.net.ConnectivityManager;import android.net.NetworkInfo;import android.os.IBinder;public class NetworkStateService extends Service {private static final String tag = "tag";private BroadcastReceiver mReceiver = new BroadcastReceiver() {@Overridepublic void onReceive(Context context, Intent intent) {String action = intent.getAction();if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {ConnectivityManager manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);NetworkInfo info = manager.getActiveNetworkInfo();if (info != null && info.isAvailable()) {Intent service = new Intent(context, LocationService.class);startService(service);} else {Intent service = new Intent(context, LocationService.class);stopService(service);}}}};@Overridepublic boolean onUnbind(Intent intent) {// TODO Auto-generated method stubreturn super.onUnbind(intent);}@Overridepublic IBinder onBind(Intent intent) {// TODO Auto-generated method stubreturn null;}@Overridepublic void onCreate() {super.onCreate();IntentFilter mFilter = new IntentFilter();mFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);registerReceiver(mReceiver, mFilter);}@Overridepublic void onDestroy() {super.onDestroy();unregisterReceiver(mReceiver);}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {return super.onStartCommand(intent, flags, startId);}}
如有必要加上开机启动
package com.mobovip.app;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;/** * BootReceiver * * @author NGJ * */public class BootReceiver extends BroadcastReceiver {@Overridepublic void onReceive(Context context, Intent intent) {// TODO Auto-generated method stubIntent i = new Intent(context, NetworkStateService.class);i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);context.startService(i);}}
以上service,BroadcastReceiver都需要注册,再加上网络权限等
<service android:enabled="true" android:name="com.mobovip.app.NetworkStateService" /> <service android:enabled="true" android:name="com.mobovip.app.LocationService" /> <!-- <receiver android:name="com.mobovip.app.BootReceiver" > <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter> </receiver> -->
0 0
- LocationListener监听位置变化,当进入到某一距离内时发出提醒
- 左边为选项卡,右边为内容,当鼠标滚轮滚动到某一位置时,选项卡样式也相应做变化
- 监听鼠标滚动事件,当滚动条滚动到某些距离时 显示DIV
- android 监听位置变化
- 打开页面时定位到某一位置
- 当ContentProvider中的数据发生变化时可以向其他用户发出通知
- HorizontalScrollView+GridView 设置初始进入页面后平滑的滚动到girdView的某一给定位置
- 当接收到消息时,亮屏震动提醒
- 实现鼠标进入进出div时位置的监听效果
- Android开发实现距离位置变化
- 我的android 第26天 - 当ContentProvider中数据发生变化时向其用户发出通知
- listview控件当点击进入一个item后返回,仍然返回到点击item时listview所定位到的位置
- 把某一路径内文件夹里的内容复制到指定位置
- 安卓开发监听recyclerview的滑动时某一控件距离顶部的距离来做显示隐藏某些布局
- 程序在4441端口监听,当检测到有客户机请求时,产生一个内为“客户,你好,我是服务器”的字符串输出到客户端
- input标签内发生变化进行监听
- ajax加载列表 当点击进入详细页再次返回列表定位到当前点击位置解决办法
- vb 锁定鼠标到某一位置
- TCP RST攻击实验流程
- CSS将图片和文字垂直居中
- Android OkHttp完全解析 是时候来了解OkHttp了
- 各种JS jQuery CSS资源等
- Android登录页面案例(SharedPreferences的使用)
- LocationListener监听位置变化,当进入到某一距离内时发出提醒
- hdu oj 6020 求给出一串数和一个K任意去点掉k个数能否被三整除(思维)
- JQM:如何点击footer中的navbar只切换content的内容
- 理解JAVA程序逻辑及面向对象编程思想简单总结(3)
- 西方文学名著鉴赏——英文诗歌翻译赏析
- 使用google map显示地图,并标注InfoWindow
- JQ插件:Knob旋钮按钮
- GreenDao教程(2):基本应用
- 缓存之整页缓存