要做一個自動短信回復,所以需要攔截系統短信.
1.在Manifest.xml里加"接收"SMS的權限
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
2.在Manifest.xml里注冊一個receive
<!-- 注冊Receiver,並且設置優先級 -->
<receiver android:name=".AutoSMS" android:exported="false">
<intent-filter android:priority="1000">
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
3.定義一個短信接收類,並且重寫onReceive
//繼承BroadcastReceiver
public class AutoSMS extends BroadcastReceiver
{
private String TAG="AutSMS";
//廣播消息類型
public static final String SMS_RECEIVED_ACTION = "android.provider.Telephony.SMS_RECEIVED";
//覆蓋onReceive方法
@Override
public void onReceive(Context context, Intent intent)
{
下面是完整的代碼:
Manifest.xml:
package ="com.xxh.autosms"
android:versionCode ="1"
android:versionName ="1.0" >
< uses-sdk
android:minSdkVersion ="8"
android:targetSdkVersion ="15" />
< uses-permission android:name ="android.permission.RECEIVE_SMS" />
< application
android:icon ="@drawable/ic_launcher"
android:label ="@string/app_name"
android:theme ="@style/AppTheme" >
< activity
android:name =".MainActivity"
android:label ="@string/title_activity_main" >
< intent-filter >
< action android:name ="android.intent.action.MAIN" />
< category android:name ="android.intent.category.LAUNCHER" />
</ intent-filter >
</ activity >
<!-- 注冊Receiver,並且設置優先級 -->
< receiver android:name =".AutoSMS" android:exported ="false" >
< intent-filter android:priority ="1000" >
< action android:name ="android.provider.Telephony.SMS_RECEIVED" />
</ intent-filter >
</ receiver >
</ application >
</ manifest >
AutoSMS.java:
import java.text.SimpleDateFormat;
import java.util.Date;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;
// 繼承BroadcastReceiver
public class AutoSMS extends BroadcastReceiver
{
private String TAG="AutSMS";
// 廣播消息類型
public static final String SMS_RECEIVED_ACTION = "android.provider.Telephony.SMS_RECEIVED";
// 覆蓋onReceive方法
@Override
public void onReceive(Context context, Intent intent)
{
// TODO Auto-generated method stub
Log.i(TAG, "引發接收事件");
// StringBuilder body=new StringBuilder(""); // 短信內容
// StringBuilder sender=new StringBuilder(""); // 發件人
// 先判斷廣播消息
String action = intent.getAction();
if (SMS_RECEIVED_ACTION.equals(action))
{
// 獲取intent參數
Bundle bundle=intent.getExtras();
// 判斷bundle內容
if (bundle!= null)
{
// 取pdus內容,轉換為Object[]
Object[] pdus=(Object[])bundle.get("pdus");
// 解析短信
SmsMessage[] messages = new SmsMessage[pdus.length];
for( int i=0;i<messages.length;i++)
{
byte[] pdu=( byte[])pdus[i];
messages[i]=SmsMessage.createFromPdu(pdu);
}
// 解析完內容后分析具體參數
for(SmsMessage msg:messages)
{
// 獲取短信內容
String content=msg.getMessageBody();
String sender=msg.getOriginatingAddress();
Date date = new Date(msg.getTimestampMillis());
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String sendTime = sdf.format(date);
// TODO:根據條件判斷,然后進一般處理
if ("10060".equals(sender))
{
// 屏蔽手機號為10060的短信,這里還可以時行一些處理,如把這個信息發送到第三人的手機等等。
// TODO:測試
Toast.makeText(context, "收到10060的短信"+"內容:"+content, Toast.LENGTH_LONG).show();
// 對於特定的內容,取消廣播
abortBroadcast();
}
else
{
Toast.makeText(context, "收到:"+sender+"內容:"+content+"時間:"+sendTime.toString(), Toast.LENGTH_LONG).show();
}
}
}
} // if 判斷廣播消息結束
}
}