获取手机所在位置经纬度,并可以响应短信,将该位置发回

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

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 provider) {// TODO Auto-generated method stub}@Overridepublic void onProviderEnabled(String provider) {// TODO Auto-generated method stub}@Overridepublic void onStatusChanged(String provider, int status, Bundle extras) {// TODO Auto-generated method stub}};@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubif(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(){@Overridepublic 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(){@Overridepublic void onReceive(Context context, Intent intent) {// TODO Auto-generated method stubToast.makeText(context, "接收完成!", Toast.LENGTH_LONG).show();}};//取消注册@Overrideprotected void onDestroy() {// TODO Auto-generated method stubsuper.onDestroy();unregisterReceiver(sendMsg);unregisterReceiver(delivery);unregisterReceiver(smsbr);}//短信接收监听器class SmsBroadcastReceiver extends BroadcastReceiver{@Overridepublic void onReceive(Context context, Intent intent) {// TODO Auto-generated method stubObject [] 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);}}}}@Overrideprotected void onStop() {// TODO Auto-generated method stubsuper.onStop();locationManager.removeUpdates(locationListener);Log.e("msg", "定位监听器停止工作!");}@Overrideprotected void onRestart() {// TODO Auto-generated method stubsuper.onRestart();locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, locationListener);Log.e("msg", "定位监听器复位!");}}

却只能这样。只有对爱的人,我们才会斤斤计较,锱铢必较。

获取手机所在位置经纬度,并可以响应短信,将该位置发回

相关文章:

你感兴趣的文章:

标签云: