我创建了一个 BroadcastReceiver,它实际上处理传入的 SMS。这是在 android 2.3.3 中运行良好的代码,但不知何故无法在运行 ICS 的设备上运行相同的代码。
我的接收器
public class SmsListener extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
System.out.println("#### SMS Received");
if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")){
Bundle bundle = intent.getExtras(); //---get the SMS message passed in---
SmsMessage[] msgs = null;
String msg_from;
if (bundle != null){
//---retrieve the SMS message received---
try{
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for(int i=0; i<msgs.length; i++){
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
msg_from = msgs[i].getOriginatingAddress();
String msgBody = msgs[i].getMessageBody();
System.out.println("#### SMS Received: " + msg_from);
System.out.println("#### Message: " + msgBody);
}
}catch(Exception e){
Log.d("Exception caught",e.getMessage());
}
}
}
}
}
权限
<receiver android:name=".SmsListener" android:enabled="true" android:permission="android.permission.RECEIVE_SMS">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS"/>