zoukankan      html  css  js  c++  java
  • [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;
                    }
                }
            }
        }
    
    }

  • 相关阅读:
    alert
    自定义基类装载数据模块显示到dataGrid
    关于dataGrid查询按钮的实现
    如何查看oracle用户具有的权限和角色 大风起
    LoadRunner使用手册 大风起
    电脑蓝屏了,教你一招保证恢复 大风起
    oracle如何查看当前有哪些用户连接到数据库 大风起
    apache+tomcat整合(动静分离) 大风起
    cdn技术浅谈 大风起
    Tomcat性能优化总结 大风起
  • 原文地址:https://www.cnblogs.com/taoshihan/p/5399066.html
Copyright © 2011-2022 走看看