WEB开发网
开发学院手机开发Android 开发 android平台获取手机所在位置经纬度.可响应短信,... 阅读

android平台获取手机所在位置经纬度.可响应短信,将该位置发回

 2013-01-03 16:01:35 来源:WEB开发网   
核心提示: 以下程序经测试没有问题,功能主要有:1)获取当前GPS经纬度信息2)其他手机发送相应短信后,android平台获取手机所在位置经纬度.可响应短信,将该位置发回,本机可以自动回复短信,以此获取到设备的经纬度信息package com.freshen.test;import android.app.Activity;im

 以下程序经测试没有问题,功能主要有:

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", "定位监听器复位!");
	}
	
}

Tags:android 平台 获取

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接