[android] 手機衛士手機實現簡訊指令擷取位置

來源:互聯網
上載者:User

標籤:

擷取位置 

建立一個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] 手機衛士手機實現簡訊指令擷取位置

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.