萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> 安卓開發 >> android平台獲取手機所在位置經緯度

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;

  @Override

  public 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(){

  @Override

  public void onLocationChanged(Location location) {

  // TODO Auto-generated method stub

  if(location!=null){

  latitude=location.getLatitude();

  longitude=location.getLongitude();

  Log.e("locationListener 經緯度", latitude+":"+longitude);

  }

  }

  @Override

  public void onProviderDisabled(String provider) {

  // TODO Auto-generated method stub

  }

  @Override

  public void onProviderEnabled(String provider) {

  // TODO Auto-generated method stub

  }

  @Override

  public void onStatusChanged(String provider, int status, Bundle extras) {

  // TODO Auto-generated method stub

  }};

  @Override

  public void onClick(View arg0) {

  // TODO Auto-generated method stub

  if(arg0.getId()==R.id.bt_getLc){

  Log.e("log", "開始獲取經緯度信息!");

  //獲取 經緯度信息

  setTitle("開始獲取經緯度>>");

  String lc=getLoaction();

  //String num=phoneNumber.getEditableText().toString();

  //Log.e("phoneNumber", num);

  //sendMsg(lc,num);

  if(latitude<1)txt.setText("獲取定位中……");

  txt.append(lc);

  }

  }

  //獲取經緯度的方法

  public String getLoaction(){

  /*

  locationManager=(LocationManager) getSystemService(Context.LOCATION_SERVICE);

  location =locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

  int i=0;

  while(location==null){

  Log.e("log", location+"");

  location =locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

  if(i++>100)break;

  }

  double latitude=location.getLatitude();

  double longitude=location.getLongitude();

  txt.setText("緯度:"+latitude +"n"+"經度:"+longitude);

  */

  return "緯度:("+latitude +")t經度:("+longitude+")n";

  }

  //發送短信

  public void sendMsg(String msg,String num){

  if(TextUtils.isEmpty(msg)||TextUtils.isEmpty(num))

  return ;

  SmsManager sms=SmsManager.getDefault();

  Intent sendIntent =new Intent("SENT_SMS_ACTION");

  PendingIntent sentPI=PendingIntent.getBroadcast(this, 0, sendIntent, 0);

  Intent deliverIntent =new Intent("DELIVERED_SMS_ACTION");

  PendingIntent deliveryPI=PendingIntent.getBroadcast(this, 0, deliverIntent, 0);

  sms.sendTextMessage(num, null, msg, sentPI, deliveryPI);

  }

  //實現注冊的 廣播服務

  private BroadcastReceiver sendMsg =new BroadcastReceiver(){

  @Override

  public void onReceive(Context context, Intent intent) {

  if(getResultCode()==Activity.RESULT_OK){

  Toast.makeText(context, "發送成功!", Toast.LENGTH_LONG).show();

  }else{

  Toast.makeText(context, "發送失敗!", Toast.LENGTH_LONG).show();

  }

  }

  };

  private BroadcastReceiver delivery=new BroadcastReceiver(){

  @Override

  public void onReceive(Context context, Intent intent) {

  // TODO Auto-generated method stub

  Toast.makeText(context, "接收完成!", Toast.LENGTH_LONG).show();

  }

  };

  //取消注冊

  @Override

  protected void onDestroy() {

  // TODO Auto-generated method stub

  super.onDestroy();

  unregisterReceiver(sendMsg);

  unregisterReceiver(delivery);

  unregisterReceiver(smsbr);

  }

  //短信接收監聽器

  class SmsBroadcastReceiver extends BroadcastReceiver{

  @Override

  public void onReceive(Context context, Intent intent) {

  // TODO Auto-generated method stub

  Object [] pdus=(Object[]) intent.getExtras().get("pdus");

  for(Object o:pdus){

  byte[] data=(byte[]) o;

  //

  SmsMessage sm=SmsMessage.createFromPdu(data);

  String sender=sm.getOriginatingAddress();

  String content=sm.getMessageBody();

  //攔截短信中含有 gpsl的短信

  if(content.contains("gpsl")){

  this.abortBroadcast();

  SmsManager smg=SmsManager.getDefault();

  smg.sendTextMessage(sender, null, getLoaction(), null, null);

  }

  }

  }

  }

copyright © 萬盛學電腦網 all rights reserved