基于Android的位置實時監(jiān)控系統(tǒng)的設計與實現
2.3 程序實現
本文引用地址:http://m.butianyuan.cn/article/273085.htm 一是定位功能的實現。充分利用Android智能手機內置GPS定位模塊,基于Eclipse開發(fā)平臺通過程序的調用,實現定位功能,手機運行結果如圖4所示。其實現代碼如下:
/**
* 將獲得的位置信息顯示出來
* @param location
*/
private void setLocation(Location location){
if (location != null) {
String latitudeStr = Double.toString(location.getLatitude());//經度
String longitudeStr = Double.toString(location.getLongitude());//緯度
String altitudeStr = Double.toString(location.getAltitude());//海拔
// 獲取當前時間
SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",Locale.CHINA);
String dateStr = sDateFormat.format(new java.util.Date());
gpsTime.setText(dateStr);
if (latitudeStr != null && !latitudeStr.equals("")) {
latitude.setText(latitudeStr);
}
if (longitudeStr != null && !longitudeStr.equals("")) {
longitude.setText(longitudeStr);
}
if (altitudeStr != null && !altitudeStr.equals("")) {
altitude.setText(altitudeStr);
}
} else {
Log.v("location", "location為空");
}
}
二是經緯度信息發(fā)送功能的實現。根據事先設置好的監(jiān)控端手機號碼,按照發(fā)送頻率(時間間隔),將經緯度發(fā)送到監(jiān)控端。其實現代碼如下:
/**
* 發(fā)送短信
* @param smsContent 短信內容
*/
private void sendSMS(String smsContent){
//獲取短信管理器
SmsManager smsManager = SmsManager.getDefault();
//拆分短信內容
List divideContents = smsManager.divideMessage(smsContent);
if (curPhoneNum == null || curPhoneNum.equals("")) {
new AlertDialog.Builder(MainActivity.this).setTitle("警告").setMessage("您還未設置目標電話號碼").create().show();
return;
}
//發(fā)送短信
for (String text : divideContents) {
smsManager.sendTextMessage(curPhoneNum, null, text, null, null);
}
}
評論