星空网 > 软件开发 > 操作系统

[android] 手机卫士手机实现短信指令获取位置

获取位置 

新建一个service的包

新建一个GPSService类继承系统的Service类

清单文件中注册一下

重写onCreate()方法,服务创建的时候回调

重写onDestroy()方法,服务销毁的时候回调

把上一节的代码拿到这个地方来

 

得到用户移动后的最后一次的位置,保存到SP中

转换标准坐标为火星坐标,数据库文件放到assets目录下,把ModifyOffset.java放在service包下面

获取ModifyOffset对象,通过ModifyOffset.getInstance()方法,参数:输入流;把资产目录下的文件转成输入流,使用getAssets().open(“文件名”)得到InputStream对象,

调用ModifyOffset对象的s2c()方法,把标准的转成中国的得到新的PointDouble对象,参数:PointDouble对象,x , y

获取到经度 PonitDouble对象的y

获取到纬度 PonitDouble对象的x

把位置数据保存到SP中

接收指令发送位置短信 

启动服务,在接收短信的地方,获取到Intent对象,调用Context对象的startService()方法

获取到SP中保存的位置信息

发送短信,SmsManager.getDefault().sendTextMessage()方法,发送短信给安全号码,参数:sendTextMessage(目标手机, null(来源手机不支持), text, sentIntent, deliveryIntent)后两个参数,延迟报告和送达报告,不关心填null

需要这个权限 android.permission.SEND_SMS

判断一下内容是否为空,如果为空发送短信内容是正在获取,手动让坐标变化一下,才能正在得到

GPSService.java

package com.qingguow.mobilesafe.service;import android.app.Service;import android.content.Intent;import android.content.SharedPreferences;import android.content.SharedPreferences.Editor;import android.location.Criteria;import android.location.Location;import android.location.LocationListener;import android.location.LocationManager;import android.os.Bundle;import android.os.IBinder;public class GPSService extends Service {  private LocationManager lm;  private LocationListener listener;  private SharedPreferences sp;  @Override  public IBinder onBind(Intent arg0) {    return null;  }  // 服务创建  @Override  public void onCreate() {    super.onCreate();    sp=getSharedPreferences("config", MODE_PRIVATE);    // 获取位置管理器    lm = (LocationManager) getSystemService(LOCATION_SERVICE);    listener = new MyLocationListener();    Criteria criteria = new Criteria();    criteria.setAccuracy(Criteria.ACCURACY_FINE);    String provider = lm.getBestProvider(criteria, true);    lm.requestLocationUpdates(provider, 0, 0, listener);      }  // 服务销毁  @Override  public void onDestroy() {    super.onDestroy();    lm.removeUpdates(listener);    listener=null;  }  private class MyLocationListener implements LocationListener {    @Override    public void onLocationChanged(Location location) {      // 获取经度      String longitude = "longitude:" + location.getLongitude();      String latitude = "latitude:" + location.getLatitude();      String acc = "accuracy:" + location.getAccuracy();      // 转换火星坐标      try {        ModifyOffset offset = ModifyOffset.getInstance(getAssets()            .open("axisoffset.dat"));        PointDouble pinit = offset.s2c(new PointDouble(location            .getLongitude(), location.getLatitude()));        longitude = "longitude:" + pinit.x;        latitude = "latitude:" + pinit.y;      } catch (Exception e) {        e.printStackTrace();      }      //保存数据      Editor editor=sp.edit();      editor.putString("lastlocation", longitude+latitude+acc);      editor.commit();    }    @Override    public void onStatusChanged(String provider, int status, Bundle extras) {    }    @Override    public void onProviderEnabled(String provider) {    }    @Override    public void onProviderDisabled(String provider) {    }  }}

 

 

SmsReceiver.java

package com.qingguow.mobilesafe.receiver;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.content.SharedPreferences;import android.media.MediaPlayer;import android.telephony.SmsManager;import android.telephony.SmsMessage;import android.text.TextUtils;import com.qingguow.mobilesafe.R;import com.qingguow.mobilesafe.service.GPSService;public class SmsReceiver extends BroadcastReceiver {  private SharedPreferences sp;  @Override  public void onReceive(Context context, Intent intent) {    sp=context.getSharedPreferences("config", Context.MODE_PRIVATE);    //获取短信内容    Object[] objs=(Object[]) intent.getExtras().get("pdus");    for(Object obj:objs){      SmsMessage sms=SmsMessage.createFromPdu((byte[])obj);      String body=sms.getMessageBody();      String sender=sms.getOriginatingAddress();      String secSender=sp.getString("secphone", "");      //判断是安全号码的短信      if(secSender.equals(sender)){        switch (body) {        case "#*alarm*#"://发送报警音乐          //Toast.makeText(context, "播放报警音乐", 1).show();          MediaPlayer mp=MediaPlayer.create(context, R.raw.alarm);          mp.start();          abortBroadcast();          break;        case "#*location*#"://得到位置信息          Intent intent1=new Intent(context,GPSService.class);          context.startService(intent1);          String lastLocation= sp.getString("lastlocation", "");          //发送短信          if(TextUtils.isEmpty(lastLocation)){            SmsManager.getDefault().sendTextMessage(sender, null,"getting location", null, null);          }else{            SmsManager.getDefault().sendTextMessage(sender, null,lastLocation, null, null);          }          System.out.println("获取位置消息"+lastLocation);          abortBroadcast();          break;        default:          break;        }      }    }  }}

[android] 手机卫士手机实现短信指令获取位置images/loading.gif' data-original="http://images2015.cnblogs.com/blog/726254/201604/726254-20160416183924566-839699178.png" />

 




原标题:[android] 手机卫士手机实现短信指令获取位置

关键词:Android

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

tiktok 插件:https://www.goluckyvip.com/tag/82515.html
tiktok ios破解:https://www.goluckyvip.com/tag/82516.html
怎么登录国外的tiktok:https://www.goluckyvip.com/tag/82517.html
tiktok 0播放:https://www.goluckyvip.com/tag/82518.html
tiktok的股份构成:https://www.goluckyvip.com/tag/82519.html
tiktok带货数据:https://www.goluckyvip.com/tag/82520.html
从创新到领航:用友YonSuite正在树立中国SaaS产业新标杆:https://www.kjdsnews.com/a/1836558.html
美属萨摩亚/American Samoa/美利坚合众国属萨摩亚:https://www.kjdsnews.com/a/1836559.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流