我不明白,当我收到短信时,没有任何内容出现。
public class MyReceiver extends BroadcastReceiver {
public MyReceiver() {
}
private static final String RECEIVE_MSG = "android.provider.Telephony.SMS_RECEIVED";
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(RECEIVE_MSG)){
Bundle extra = intent.getExtras();
if(extra!= null){
Object[] pdus = (Object[])extra.get("pdus");
final SmsMessage[] messages = new SmsMessage[pdus.length];
for(int i = 0; i<pdus.length; i++){
messages[i]= SmsMessage.createFromPdu((byte[])pdus[i]);
}
if (messages.length>-1) {
for (int i = 0; i<messages.length; i++) {
final String messageBody = messages[i].getMessageBody();
final String phoneNumber = messages[i].getDisplayOriginatingAddress();
Toast.makeText(context,"Expéditeur:" + phoneNumber, Toast.LENGTH_LONG).show();
Toast.makeText(context,"Message : " + messageBody, Toast.LENGTH_LONG).show();
}
}
}
}
}
}
答案 0 :(得分:0)
您是在清单或活动中注册了reicever吗?
<receiver android:name=".broadcast.SmsBroadcastReceiver" >
<intent-filter android:priority="5000">
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
或
IntentFilter filter = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");
filter.setPriority(9999);
registerReceiver(new SmsBroadcastReceiver(),filter);