推薦閱讀:
深入淺析Android手機(jī)衛(wèi)士保存密碼時(shí)進(jìn)行md5加密
詳解Android 手機(jī)衛(wèi)士設(shè)置向?qū)ы?yè)面
淺析Android手機(jī)衛(wèi)士關(guān)閉自動(dòng)更新
淺析Android手機(jī)衛(wèi)士自定義控件的屬性
淺析Android手機(jī)衛(wèi)士讀取聯(lián)系人
淺析Android手機(jī)衛(wèi)士接收短信指令執(zhí)行相應(yīng)操作
淺析Android手機(jī)衛(wèi)士手機(jī)定位的原理
獲取位置
新建一個(gè)service的包
新建一個(gè)GPSService類(lèi)繼承系統(tǒng)的Service類(lèi)
清單文件中注冊(cè)一下
重寫(xiě)onCreate()方法,服務(wù)創(chuàng)建的時(shí)候回調(diào)
重寫(xiě)onDestroy()方法,服務(wù)銷(xiāo)毀的時(shí)候回調(diào)
把上一節(jié)的代碼拿到這個(gè)地方來(lái)
得到用戶(hù)移動(dòng)后的最后一次的位置,保存到SP中
轉(zhuǎn)換標(biāo)準(zhǔn)坐標(biāo)為火星坐標(biāo),數(shù)據(jù)庫(kù)文件放到assets目錄下,把ModifyOffset.java放在service包下面
獲取ModifyOffset對(duì)象,通過(guò)ModifyOffset.getInstance()方法,參數(shù):輸入流;把資產(chǎn)目錄下的文件轉(zhuǎn)成輸入流,使用getAssets().open(“文件名”)得到InputStream對(duì)象,
調(diào)用ModifyOffset對(duì)象的s2c()方法,把標(biāo)準(zhǔn)的轉(zhuǎn)成中國(guó)的得到新的PointDouble對(duì)象,參數(shù):PointDouble對(duì)象,x , y
獲取到經(jīng)度 PonitDouble對(duì)象的y
獲取到緯度 PonitDouble對(duì)象的x
把位置數(shù)據(jù)保存到SP中
接收指令發(fā)送位置短信
啟動(dòng)服務(wù),在接收短信的地方,獲取到Intent對(duì)象,調(diào)用Context對(duì)象的startService()方法
獲取到SP中保存的位置信息
發(fā)送短信,SmsManager.getDefault().sendTextMessage()方法,發(fā)送短信給安全號(hào)碼,參數(shù):sendTextMessage(目標(biāo)手機(jī), null(來(lái)源手機(jī)不支持), text, sentIntent, deliveryIntent)后兩個(gè)參數(shù),延遲報(bào)告和送達(dá)報(bào)告,不關(guān)心填null
需要這個(gè)權(quán)限 android.permission.SEND_SMS
判斷一下內(nèi)容是否為空,如果為空發(fā)送短信內(nèi)容是正在獲取,手動(dòng)讓坐標(biāo)變化一下,才能正在得到
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;@Overridepublic IBinder onBind(Intent arg0) {return null;}// 服務(wù)創(chuàng)建@Overridepublic 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);}// 服務(wù)銷(xiāo)毀@Overridepublic void onDestroy() {super.onDestroy();lm.removeUpdates(listener);listener=null;}private class MyLocationListener implements LocationListener {@Overridepublic void onLocationChanged(Location location) {// 獲取經(jīng)度String longitude = "longitude:" + location.getLongitude();String latitude = "latitude:" + location.getLatitude();String acc = "accuracy:" + location.getAccuracy();// 轉(zhuǎn)換火星坐標(biāo)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();}//保存數(shù)據(jù)Editor editor=sp.edit();editor.putString("lastlocation", longitude+latitude+acc);editor.commit();}@Overridepublic void onStatusChanged(String provider, int status, Bundle extras) {}@Overridepublic void onProviderEnabled(String provider) {}@Overridepublic 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;@Overridepublic void onReceive(Context context, Intent intent) {sp=context.getSharedPreferences("config", Context.MODE_PRIVATE);//獲取短信內(nèi)容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", "");//判斷是安全號(hào)碼的短信if(secSender.equals(sender)){switch (body) {case "#*alarm*#"://發(fā)送報(bào)警音樂(lè)//Toast.makeText(context, "播放報(bào)警音樂(lè)", 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", "");//發(fā)送短信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手機(jī)衛(wèi)士之手機(jī)實(shí)現(xiàn)短信指令獲取位置的相關(guān)內(nèi)容,希望對(duì)大家有所幫助!
新聞熱點(diǎn)
疑難解答
圖片精選