如何获取方法中收到的当前消息:
public class RecieveSMS extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(SMS_RECEIVED)) {
try {
我如何检索号码,时间和身体?当我试图保存消息然后中止广播我尝试查询内容://短信但是如果它们不是来自该用户的消息它接缝接收第一条消息然后阻止其余的。
答案 0 :(得分:1)
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String messageContent = "";
String number = "";
if (bundle != null)
{
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]);
number = msgs[i].getOriginatingAddress();
messageContent = msgs[i].getMessageBody().toString();
}
}
}
答案 1 :(得分:1)
检查一下:为SMS创建广播接收器的示例: