我有一个短信传入电话号码123456
。我想用Intent在服务中打开传入电话号码的SMS消息。我尝试了两种方式,但他们给不满意结果在阅读模式Android 5.0中无法阅读短信?
第一种方式:
Intent smsIntent = new Intent(Intent.ACTION_MAIN);
smsIntent.addCategory(Intent.CATEGORY_DEFAULT);
smsIntent.setType("vnd.android-dir/mms-sms");
smsIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(smsIntent);
的方式只打开短信列表。我们必须查看列表中的传入电话,并需要一个步骤才能打开传入的SMS电话的内容。我想忽略这一步。这意味着它会直接转到收到的短信电话号码的内容。
方式二:
Intent smsIntent = new Intent(Intent.ACTION_VIEW);
smsIntent.setType("vnd.android-dir/mms-sms");
smsIntent.putExtra("address", SMSphoneNumber);
smsIntent.putExtra("sms_body",SMSBody);
smsIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(smsIntent);
这样可以读取传入的短信电话号码的细节,但它在发送模式。我想在阅读模式下阅读短信。
如何在Android L中执行此操作?谢谢大家
这个问题与How to open SMS intent to read (not send) message?有关。不同的是为两种情况提供源代码:一种是阅读模式(第一种),需要多一步,另一种是发送模式。
更新:这是什么方式我得到的短信来电号码
public class SMSReceiver extends BroadcastReceiver {
private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(SMS_RECEIVED)) {
Bundle extras = intent.getExtras();
SmsMessage[] smgs = null;
String infoSender = "";
String infoSMS = "";
if (extras != null) {
// Retrieve the sms message received
Object[] pdus = (Object[]) extras.get("pdus");
smgs = new SmsMessage[pdus.length];
for (int i = 0; i < smgs.length; i++) {
smgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
infoSender += smgs[i].getOriginatingAddress();
if (smgs[i].getMessageBody()!=null)
infoSMS += smgs[i].getMessageBody().toString();
else
infoSMS += ".";
}
Toast.makeText(context, "Phone in SMSReceiver is -" + infoSender + "Body is" + infoSMS, Toast.LENGTH_SHORT).show();
}
}
}
}
你的问题不清楚。你想获得数字和消息正文,并过滤一个特定的数字? –
我已经有了BroadcastReceiver的号码和身体。我只想在阅读模式下打开传入电话号码的短信。 – Jame
可能重复[如何打开SMS意图阅读(不发送)消息?](http://stackoverflow.com/questions/39678042/how-to-open-sms-intent-to-read-not-send-消息) –