LBS趋近报警

简介: 项目目录   类代码   Mylocation.java   package com.swift.mylocation; import java.io.IOException; import java.

项目目录

 

类代码

 

Mylocation.java

 


package com.swift.mylocation;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

import android.app.PendingIntent;

import android.content.Context;
import android.content.Intent;

import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

 

import android.widget.TextView;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.swift.mylocation.R;

public class Mylocation extends MapActivity {

 MapController mapController;
 List<Overlay> overlays;
 MyPositionOverlay positionOverlay;
 LocationManager locationManager;
 MapView myMapView;

 final String PROXIMITY_ALERT = new String(
   "android.intent.action.proximityalert");

 @SuppressWarnings("deprecation")
 @Override
 public void onCreate(Bundle icicle) {
  super.onCreate(icicle);
  setContentView(R.layout.main);

  myMapView = (MapView) findViewById(R.id.myMapView);

  mapController = myMapView.getController();

  myMapView.setSatellite(true);
  myMapView.setStreetView(true);
  myMapView.displayZoomControls(false);

  mapController.setZoom(17);

  positionOverlay = new MyPositionOverlay();
  overlays = myMapView.getOverlays();
  overlays.add(positionOverlay);

  Criteria criteria = new Criteria();
  criteria.setAccuracy(Criteria.ACCURACY_FINE);
  criteria.setAltitudeRequired(true);
  criteria.setBearingRequired(false);
  criteria.setCostAllowed(false);
  criteria.setPowerRequirement(Criteria.POWER_LOW);
  locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

  String provider = locationManager.getBestProvider(criteria, true);

  Location location = locationManager.getLastKnownLocation(provider);

  updateWithNewLocation(location);

  locationManager.requestLocationUpdates(provider, 2000, 10,
    locationListener);
 
  setProximityAlert();

 }

 private final LocationListener locationListener = new LocationListener() {
  @Override
  public void onLocationChanged(Location location) {
   updateWithNewLocation(location);
  }

  @Override
  public void onProviderDisabled(String provider) {
   updateWithNewLocation(null);
  }

  @Override
  public void onProviderEnabled(String provider) {
  }

  @Override
  public void onStatusChanged(String provider, int status, Bundle extras) {
  }
 };

 
 

 void setProximityAlert() {
  
  double lat = 31.620356666666666;
  double lng = 121.38631333333333;
  float radius = 50f;
  long expiration = -1;

  Intent intent = new Intent(PROXIMITY_ALERT);
  intent.setAction(PROXIMITY_ALERT);
  PendingIntent proximityIntent = PendingIntent.getBroadcast(this, -1,
    intent, 0);

  locationManager.addProximityAlert(lat, lng, radius, expiration,
    proximityIntent);

 }

 /** Update UI with a new location */
 private void updateWithNewLocation(Location location) {

  String latLongString;
  TextView myLocationText;
  myLocationText = (TextView) findViewById(R.id.myLocationText);
  String addressString = "No address found";
  if (location != null) {

   positionOverlay.setLocation(location);

   Double geoLat = location.getLatitude() * 1E6;
   Double geoLng = location.getLongitude() * 1E6;
   GeoPoint point = new GeoPoint(geoLat.intValue(), geoLng.intValue());
   mapController.animateTo(point);

   double lat = location.getLatitude();
   double lng = location.getLongitude();
   latLongString = "Lat:" + lat + "\nLong:" + lng;
   double latitude = location.getLatitude();
   double longitude = location.getLongitude();

   Geocoder gc = new Geocoder(this, Locale.CHINA);
   try {
    List<Address> addresses = gc.getFromLocation(latitude,
      longitude, 1);
    StringBuilder sb = new StringBuilder();
    if (addresses.size() > 0) {
     Address address = addresses.get(0);
     for (int i = 0; i < address.getMaxAddressLineIndex(); i++)
      sb.append(address.getAddressLine(i)).append("\n");
     sb.append(address.getCountryName());
     sb.append(address.getLocality()).append("\n");
    }
    addressString = sb.toString();
   } catch (IOException e) {
   }
  } else {
   latLongString = "No location found";
  }
  myLocationText.setText("Your Current Position is:\n" + latLongString
    + "\n" + addressString);
  
  Toast.makeText(this,this.getIntent().getStringExtra(PROXIMITY_ALERT) , Toast.LENGTH_LONG);
 }

 @Override
 protected boolean isRouteDisplayed() {
  return true;
 }
}

 

MyPostionOverlay.java

 


package com.swift.mylocation;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.RectF;
import android.location.Location;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;

public class MyPositionOverlay extends Overlay {

 Location location, mlocation;
 final Double lat = 31.620356666666666 * 1E6,
   lon = 121.38631333333333 * 1E6;
 final int ccc = 50;
 private final int mRadius = 5;

 /**
  * @return the location
  */
 public Location getLocation() {
  return location;
 }

 /**
  * @param location
  *            the location to set
  */
 public void setLocation(Location location) {
  this.location = location;
 }

 @Override
 public void draw(Canvas canvas, MapView mapView, boolean shadow) {
  Projection projection = mapView.getProjection();
  if (shadow == false) {

   Double latitude = location.getLatitude() * 1E6;
   Double longitude = location.getLongitude() * 1E6;
   GeoPoint geoPoint, gp;
   geoPoint = new GeoPoint(latitude.intValue(), longitude.intValue());
   gp = new GeoPoint(lat.intValue(), lon.intValue());

   Point point = new Point();
   projection.toPixels(geoPoint, point);
   RectF oval = new RectF(point.x - mRadius, point.y - mRadius,
     point.x + mRadius, point.y + mRadius);

   Paint paint = new Paint();
   paint.setARGB(250, 255, 0, 0);
   paint.setAntiAlias(true);
   paint.setFakeBoldText(true);
   Paint backPaint = new Paint();
   backPaint.setARGB(175, 50, 50, 50);
   backPaint.setAntiAlias(true);
   RectF backRect = new RectF(point.x + 2 + mRadius, point.y - 3
     * mRadius, point.x + 65, point.y + mRadius);

   Point center = new Point();

   projection.toPixels(gp, center);

   Paint ccp = new Paint();
   ccp.setARGB(250, 0, 0, 255);
   ccp.setAntiAlias(true);

   canvas.drawCircle(center.x, center.y, ccc, ccp);

   canvas.drawOval(oval, paint);
   canvas.drawRoundRect(backRect, 5, 5, backPaint);
   canvas.drawText("Here I Am", point.x + 2 * mRadius, point.y, paint);

  }
  super.draw(canvas, mapView, shadow);
 }

 @Override
 public boolean onTap(GeoPoint point, MapView mapView) {
  return false;
 }
}

 

ProximityIntentReceiver.java

 

package com.swift.mylocation;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.location.LocationManager;
import android.util.Log;

public class ProximityIntentReceiver extends BroadcastReceiver {

  private final String textin = "alert : in", textout = "alert : out";

  @Override
  public void onReceive(Context context, Intent intent) {
   String key = LocationManager.KEY_PROXIMITY_ENTERING;
   Boolean isEnter = intent.getBooleanExtra(key, false);
   if (isEnter) {
    Log.d("allan", textin);
    
    
   } else {
    Log.d("allan", textout);
    
    
    
   }

  }

 }

 

 

 

main.xml

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
  <TextView 
    android:id="@+id/myLocationText"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
  />
 
  
  <com.google.android.maps.MapView
 android:id="@+id/myMapView"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:enabled="true"
 android:clickable="true"
 android:apiKey="yourApiKey"
/>
</LinearLayout>

 

androidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.swift.mylocation">
  <application android:icon="@drawable/icon">
   <uses-library android:name="com.google.android.maps"/>
    <activity android:name="com.swift.mylocation.Mylocation" android:label="@string/app_name">
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
    </activity>
   
    <receiver android:name="ProximityIntentReceiver">
     <intent-filter>
      <action android:name="android.intent.action.proximityalert" />      
     </intent-filter>
    </receiver>
   
  </application>     

  <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
  <uses-permission android:name="android.permission.INTERNET"/>
  <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
  <uses-sdk android:minSdkVersion="3"/>
</manifest>

 

 

 

相关文章
|
2天前
|
云安全 人工智能 安全
AI被攻击怎么办?
阿里云提供 AI 全栈安全能力,其中对网络攻击的主动识别、智能阻断与快速响应构成其核心防线,依托原生安全防护为客户筑牢免疫屏障。
|
12天前
|
域名解析 人工智能
【实操攻略】手把手教学,免费领取.CN域名
即日起至2025年12月31日,购买万小智AI建站或云·企业官网,每单可免费领1个.CN域名首年!跟我了解领取攻略吧~
|
6天前
|
安全 Java Android开发
深度解析 Android 崩溃捕获原理及从崩溃到归因的闭环实践
崩溃堆栈全是 a.b.c?Native 错误查不到行号?本文详解 Android 崩溃采集全链路原理,教你如何把“天书”变“说明书”。RUM SDK 已支持一键接入。
485 201
|
4天前
|
人工智能 移动开发 自然语言处理
2025最新HTML静态网页制作工具推荐:10款免费在线生成器小白也能5分钟上手
晓猛团队精选2025年10款真正免费、无需编程的在线HTML建站工具,涵盖AI生成、拖拽编辑、设计稿转代码等多种类型,均支持浏览器直接使用、快速出图与文件导出,特别适合零基础用户快速搭建个人网站、落地页或企业官网。
603 157
|
4天前
|
数据采集 消息中间件 人工智能
跨系统数据搬运的全方位解析,包括定义、痛点、技术、方法及智能体解决方案
跨系统数据搬运打通企业数据孤岛,实现CRM、ERP等系统高效互通。伴随数字化转型,全球市场规模超150亿美元,中国年增速达30%。本文详解其定义、痛点、技术原理、主流方法及智能体新范式,结合实在Agent等案例,揭示从数据割裂到智能流通的实践路径,助力企业降本增效,释放数据价值。
|
10天前
|
人工智能 自然语言处理 安全
国内主流Agent工具功能全维度对比:从技术内核到场景落地,一篇读懂所有选择
2024年全球AI Agent市场规模达52.9亿美元,预计2030年将增长至471亿美元,亚太地区增速领先。国内Agent工具呈现“百花齐放”格局,涵盖政务、金融、电商等多场景。本文深入解析实在智能实在Agent等主流产品,在技术架构、任务规划、多模态交互、工具集成等方面进行全维度对比,结合市场反馈与行业趋势,为企业及个人用户提供科学选型指南,助力高效落地AI智能体应用。
|
存储 人工智能 监控
从代码生成到自主决策:打造一个Coding驱动的“自我编程”Agent
本文介绍了一种基于LLM的“自我编程”Agent系统,通过代码驱动实现复杂逻辑。该Agent以Python为执行引擎,结合Py4j实现Java与Python交互,支持多工具调用、记忆分层与上下文工程,具备感知、认知、表达、自我评估等能力模块,目标是打造可进化的“1.5线”智能助手。
604 46