android平台擷取手機所在位置經緯度

來源:互聯網
上載者:User

  以下程式經測試沒有問題,功能主要有:

  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);

  }

  }

  }

  }

  @Override

  protected void onStop() {

  // TODO Auto-generated method stub

  super.onStop();

  locationManager.removeUpdates(locationListener);

  Log.e("msg", "定位監聽器停止工作!");

  }

  @Override

  protected void onRestart() {

  // TODO Auto-generated method stub

  super.onRestart();

  locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, locationListener);

  Log.e("msg", "定位監聽器複位!");

  }

  }

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.