【3D技术宅公社】XR数字艺术论坛  XR技术讨论 XR互动电影 定格动画

 找回密码
 立即注册

QQ登录

只需一步,快速开始

调查问卷
论坛即将给大家带来全新的技术服务,面向三围图形学、游戏、动画的全新服务论坛升级为UTF8版本后,中文用户名和用户密码中有中文的都无法登陆,请发邮件到324007255(at)QQ.com联系手动修改密码

3D技术论坛将以计算机图形学为核心,面向教育 推出国内的三维教育引擎该项目在持续研发当中,感谢大家的关注。

查看: 1279|回复: 0

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

[复制链接]
发表于 2013-1-15 17:02:20 | 显示全部楼层 |阅读模式

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

1)获取当前GPS经纬度信息

2)其他手机发送相应短信后,本机可以自动回复短信,以此获取到设备的经纬度信息

  1. package com.freshen.test;

  2. import android.app.Activity;
  3. import android.app.PendingIntent;
  4. import android.content.BroadcastReceiver;
  5. import android.content.Context;
  6. import android.content.Intent;
  7. import android.content.IntentFilter;
  8. import android.location.Location;
  9. import android.location.LocationListener;
  10. import android.location.LocationManager;
  11. import android.os.Bundle;
  12. import android.telephony.SmsManager;
  13. import android.telephony.SmsMessage;
  14. import android.text.TextUtils;
  15. import android.text.method.ScrollingMovementMethod;
  16. import android.util.Log;
  17. import android.view.View;
  18. import android.view.View.OnClickListener;
  19. import android.widget.Button;
  20. import android.widget.EditText;
  21. import android.widget.TextView;
  22. import android.widget.Toast;

  23. public class TestLocationActivity extends Activity implements OnClickListener{
  24.         TextView txt;
  25.         Button getLc;
  26.         EditText phoneNumber;
  27.        
  28.         //定位信息
  29.         LocationManager locationManager;
  30.         Location location;
  31.         //发送短信
  32.         Context mContext=null;
  33.         //接收短信的广播
  34.         SmsBroadcastReceiver smsbr;
  35.         //经纬度
  36.         double latitude,longitude;
  37.        
  38.        
  39.         @Override
  40.     public void onCreate(Bundle savedInstanceState) {
  41.         super.onCreate(savedInstanceState);
  42.         setContentView(R.layout.main);
  43.         mContext=this;
  44.         //
  45.         txt=(TextView) findViewById(R.id.tv_txt);
  46.         txt.setMovementMethod(ScrollingMovementMethod.getInstance());
  47.         getLc=(Button) findViewById(R.id.bt_getLc);
  48.         phoneNumber=(EditText) findViewById(R.id.phone);
  49.         
  50.         getLc.setOnClickListener(this);
  51.         //注册短信发送与对方接收到 广播 消息
  52.         registerReceiver(sendMsg, new IntentFilter("SENT_SMS_ACTION"));
  53.         registerReceiver(delivery, new IntentFilter("DELIVERED_SMS_ACTION"));
  54.         //注册接收短信广播
  55.         IntentFilter smsitf=new IntentFilter("android.provider.Telephony.SMS_RECEIVED");
  56.         smsitf.setPriority(10000);
  57.         smsbr=new SmsBroadcastReceiver();
  58.         registerReceiver(smsbr,smsitf);
  59.         /*位置获取经纬度*/
  60.         //位置管理器 实例
  61.         locationManager=(LocationManager) getSystemService(LOCATION_SERVICE);
  62.         //位置更新 监听器注册
  63.         locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, locationListener);
  64.         
  65.     }
  66.         //LocationListener位置变化的监听器
  67.         private final LocationListener locationListener=new LocationListener(){
  68.                 @Override
  69.                 public void onLocationChanged(Location location) {
  70.                         // TODO Auto-generated method stub
  71.                         if(location!=null){
  72.                                 latitude=location.getLatitude();
  73.                                 longitude=location.getLongitude();
  74.                                 Log.e("locationListener 经纬度", latitude+":"+longitude);
  75.                         }
  76.                 }

  77.                 @Override
  78.                 public void onProviderDisabled(String provider) {
  79.                         // TODO Auto-generated method stub
  80.                        
  81.                 }

  82.                 @Override
  83.                 public void onProviderEnabled(String provider) {
  84.                         // TODO Auto-generated method stub
  85.                        
  86.                 }

  87.                 @Override
  88.                 public void onStatusChanged(String provider, int status, Bundle extras) {
  89.                         // TODO Auto-generated method stub
  90.                        
  91.                 }};
  92.        
  93.         @Override
  94.         public void onClick(View arg0) {
  95.                 // TODO Auto-generated method stub
  96.                 if(arg0.getId()==R.id.bt_getLc){
  97.                         Log.e("log", "开始获取经纬度信息!");
  98.                         //获取 经纬度信息
  99.                         setTitle("开始获取经纬度>>");
  100.                         String lc=getLoaction();
  101.                         //String num=phoneNumber.getEditableText().toString();
  102.                         //Log.e("phoneNumber", num);
  103.                         //sendMsg(lc,num);
  104.                         if(latitude<1)txt.setText("获取定位中……");
  105.                         txt.append(lc);
  106.                 }
  107.         }
  108.         //获取经纬度的方法
  109.         public String getLoaction(){
  110.                 /*
  111.                 locationManager=(LocationManager) getSystemService(Context.LOCATION_SERVICE);
  112.                 location =locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
  113.                 int i=0;
  114.                 while(location==null){
  115.                         Log.e("log", location+"");
  116.                         location =locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
  117.                         if(i++>100)break;
  118.                 }
  119.                 double latitude=location.getLatitude();
  120.                 double longitude=location.getLongitude();
  121.                 txt.setText("纬度:"+latitude +"\n"+"经度:"+longitude);
  122.                 */
  123.                
  124.                 return "纬度:("+latitude +")\t经度:("+longitude+")\n";
  125.         }
  126.         //发送短信
  127.         public void sendMsg(String msg,String num){
  128.                 if(TextUtils.isEmpty(msg)||TextUtils.isEmpty(num))
  129.                         return ;
  130.                 SmsManager sms=SmsManager.getDefault();
  131.                
  132.                 Intent sendIntent =new Intent("SENT_SMS_ACTION");
  133.                 PendingIntent sentPI=PendingIntent.getBroadcast(this, 0, sendIntent, 0);
  134.                
  135.                 Intent deliverIntent =new Intent("DELIVERED_SMS_ACTION");
  136.                 PendingIntent deliveryPI=PendingIntent.getBroadcast(this, 0, deliverIntent, 0);
  137.                
  138.                 sms.sendTextMessage(num, null, msg, sentPI, deliveryPI);
  139.                
  140.         }
  141.         //实现注册的 广播服务
  142.         private BroadcastReceiver sendMsg =new BroadcastReceiver(){
  143.                 @Override
  144.                 public void onReceive(Context context, Intent intent) {
  145.                         if(getResultCode()==Activity.RESULT_OK){
  146.                                 Toast.makeText(context, "发送成功!", Toast.LENGTH_LONG).show();
  147.                         }else{
  148.                                 Toast.makeText(context, "发送失败!", Toast.LENGTH_LONG).show();
  149.                         }
  150.                 }
  151.         };
  152.         private BroadcastReceiver delivery=new BroadcastReceiver(){
  153.                 @Override
  154.                 public void onReceive(Context context, Intent intent) {
  155.                         // TODO Auto-generated method stub
  156.                         Toast.makeText(context, "接收完成!", Toast.LENGTH_LONG).show();
  157.                 }
  158.                
  159.         };
  160.         //取消注册
  161.         @Override
  162.         protected void onDestroy() {
  163.                 // TODO Auto-generated method stub
  164.                 super.onDestroy();
  165.                 unregisterReceiver(sendMsg);
  166.                 unregisterReceiver(delivery);
  167.                 unregisterReceiver(smsbr);
  168.         }
  169.        
  170.         //短信接收监听器
  171.         class SmsBroadcastReceiver extends BroadcastReceiver{
  172.                 @Override
  173.                 public void onReceive(Context context, Intent intent) {
  174.                         // TODO Auto-generated method stub
  175.                         Object [] pdus=(Object[]) intent.getExtras().get("pdus");
  176.                         for(Object o:pdus){
  177.                                 byte[] data=(byte[]) o;
  178.                                 //
  179.                                 SmsMessage sm=SmsMessage.createFromPdu(data);
  180.                                 String sender=sm.getOriginatingAddress();
  181.                                 String content=sm.getMessageBody();
  182.                                
  183.                                 //拦截短信中含有 gpsl的短信
  184.                                 if(content.contains("gpsl")){
  185.                                         this.abortBroadcast();
  186.                                         SmsManager smg=SmsManager.getDefault();
  187.                                         smg.sendTextMessage(sender, null, getLoaction(), null, null);
  188.                                 }
  189.                         }
  190.                 }
  191.         }

  192.         @Override
  193.         protected void onStop() {
  194.                 // TODO Auto-generated method stub
  195.                 super.onStop();
  196.                 locationManager.removeUpdates(locationListener);
  197.                 Log.e("msg", "定位监听器停止工作!");
  198.         }
  199.         @Override
  200.         protected void onRestart() {
  201.                 // TODO Auto-generated method stub
  202.                 super.onRestart();
  203.                 locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, locationListener);
  204.                 Log.e("msg", "定位监听器复位!");
  205.         }
  206.        
  207. }
复制代码
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|3D数字艺术论坛 ( 沪ICP备14023054号 )

GMT+8, 2024-9-29 05:15

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表