服务程序
package com.tang.monitosms;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.SmsMessage;
public class SmsMonitorReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
System.out.println("收到短信了");
Object[] pdus = (Object[]) intent.getExtras().get("pdus");
for (Object pdu : pdus) {
SmsMessage msg = SmsMessage.createFromPdu((byte[]) pdu);
String body = msg.getMessageBody();
String address = msg.getOriginatingAddress();
System.out.println(address+"-----"+body);
}
}
}
清单文件
<receiver android:name=".SmsMonitorReceiver">
<intent-filter >
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
权限
<uses-permission android:name="android.permission.RECEIVE_SMS"/>