防盗之获取经纬度防盗之获取经纬度(Android)获取经纬度有 gps , network , 基站 三种方式,我们可以根据定位的条件,获取一个最好的定位方式然后将获取到经纬度信息发送到指定的号码中/** 单态只允许存在一个实例.* 获取的 gps 信息 */ public class GPSInfoService { private Context context; private LocationManager manager; SharedPreferences sp ; //私有化构造方法 private GPSInfoService(Context context){ this.context= context; manager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); sp = context.getSharedPreferences(“config“, Context.MODE_PRIVATE); } private static GPSInfoService mGPSService; public synchronized static GPSInfoService getInstance(Context context){ if(mGPSService==null) mGPSService = new GPSInfoService(context); return mGPSService; } /** 当前你的 所支持的定位方式获取出来 * 有多种定位方式 gps network ,基站, passive* 可以根据定位的条件,获取 一个最好的定位方式 */ public void registerLocationUpdates(){ Criteria criteria = new Criteria(); // 设置定位的精度 criteria.setAccuracy(Criteria.ACCURACY_COARSE); //获取大体的位置 criteria.setAltitudeRequired(false); // 海拔信息 criteria.setCostAllowed(true); //允许产生费用 criteria.setPowerRequirement(Criteria.POWER_LOW); //低功耗 //获取一个最符合查询条件的位置提供者 String provider =manager.getBestProvider(criteria, true); // 位置改变就会调用 Linster 的监听器 获取经度纬度 manager.requestLocationUpdates(provider, 60000, 0, getLinster()); } public void cancleLocationUpdates(){ manager.removeUpdates(getLinster()); } private static MyGPSLinster myGPSLinser; private MyGPSLinster getLinster(){ if(myGPSLinser==null) myGPSLinser = new MyGPSLinster(); return myGPSLinser; } private class MyGPSLinster implements LocationListener{ // 用户位置改变的时候 的回调方法 public void onLocationChanged(Location location) { //获取到用户的纬度 double latitude= location.getLatitude(); //获取到用户的经度 double longitude = location.getLongitude(); //进行封装写入到文件中 String locationstr = “jing du “+ longitude + “ weidu :“+latitude; Editor editor = sp.edit(); editor.putString(“lastlocation“, locationstr); mit(); } // 状态改变 public void onStatusChanged(String provider, int status, Bundle extras) { // TODO Auto-generated method stub } //gps ,打开 public void onProviderEnabled(String provider) { // TODO Auto-generated method stub } //关闭 public void onProviderDisabled(String provider) { // TODO Auto-generated method stub } } /*** 获取的最后一次位置 * @return*/ public String getLastPosition(){ return sp.getString(“lastlocation“, ““); } } 获取短信的经纬度并将获取到的经纬度发送到指定的号码上://获取当前的经纬度. GPSInfoService.getInstance(context).registerLocationUpdates(); //把经纬度的信息发送到安全号码,获取到短信发送器,将短信发送到指定的号码 SmsManager smsManager = SmsManager.getDefault(); smsManager.sendTextMessage(“15287978798“, null, GPSInfoService.getInstance(context).getLastPosition() , null, null); 摘自 傅荣康专栏android 平台获取所在位置经纬度平台获取所在位置经纬度以下程序经测试没有问题,功能主要有:1)获取当前 GPS 经纬度信息2)其他发送相应短信后,本机可以自动回复短信,以此获取到设备的经纬度信息package com.freshen.test;import android.app.Activity;import android.app.PendingIntent;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.content.IntentFilter;import android.location.Location;import android.location.LocationListener;import android.location.LocationManager;import android.os.Bundle;import android.telephony.SmsManager;import android.telephony.SmsMessage;import android.text.TextUtils;import android.text.method.ScrollingMovementMethod;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;import android.widget.Toast;public class TestLocationActivity extends Activity implements OnClickListener{TextView txt;Button getLc;EditText phoneNumber;//定位信息LocationManager locationManager;Location location;//发送短信Context mContext=null;//接收短信的广播SmsBroadcastReceiver smsbr;//经纬度double latitude,longitude;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);mContext=this;//txt=(TextView) findViewById(R.id.tv_txt);txt.setMovementMethod(ScrollingMovementMethod.getInstance());getLc=(Button) findViewById(R.id.bt_getLc);phoneNumber=(EditText) findViewById(R.id.phone);getLc.setOnClickListener(this);//注册短信发送与对方接收到 广播 消息registerReceiver(sendMsg, new IntentFilter(“SENT_SMS_ACTION“));registerReceiver(delivery, new IntentFilter(“DELIVERED_SMS_ACTION“));//注册接收短信广播IntentFilter smsitf=new IntentFilter(“android.provider.Telephony.SMS_RECEIVED“);smsitf.setPriority(10000);smsbr=new SmsBroadcastReceiver();registerReceiver(smsbr,smsitf);/*位置获取经纬度*///位置管理器 实例locationManager=(LocationManager) getSystemService(LOCATION_SERVICE);//位置更新 监听器注册locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, locationListener);}//LocationListener 位置变化的监听器private final LocationListener locationListener=new LocationListener(){@Overridepublic void onLocationChanged(Location location) {// TODO Auto-generated method stubif(location!=null){latitude=location.getLatitude();longitude=location.getLongitude();Log.e(“locationListener 经纬度“, latitude+“:“+longitude);}}@Overridepublic void onProviderDisabled(String p。