具有额外参数格式的SMSMessage createFromPdu

时间:2015-10-01 11:13:34

标签: android android-6.0-marshmallow

我应该传递什么作为第二个参数<“format”> to createFromPdu()方法,

SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i], format);

在最新版本的android中,以下代码行已弃用,

SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);

我在互联网上搜索过,但对我来说并不清楚。我也读过android文档,

http://developer.android.com/reference/android/telephony/SmsMessage.html#createFromPdu(byte[],java.lang.String)

1 个答案:

答案 0 :(得分:51)

基本上这是为Android Marshmallow引入的,以支持" 3gpp"用于3GPP格式的GSM / UMTS / LTE消息或" 3gpp2"用于3GPP2格式的CDMA / LTE消息。

以下是SMSReceiver的完整示例:

public class SMSReceiver extends BroadcastReceiver {

public void onReceive(Context context, Intent intent)
{
    Bundle myBundle = intent.getExtras();
    SmsMessage [] messages = null;
    String strMessage = "";

    if (myBundle != null)
    {
        Object [] pdus = (Object[]) myBundle.get("pdus");

        messages = new SmsMessage[pdus.length];

        for (int i = 0; i < messages.length; i++)
        {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                String format = myBundle.getString("format");
                messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i], format);
            }
            else {
                messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
            }
            strMessage += "SMS From: " + messages[i].getOriginatingAddress();
            strMessage += " : ";
            strMessage += messages[i].getMessageBody();
            strMessage += "\n";
        }

        Log.e("SMS", strMessage);
        Toast.makeText(context, strMessage, Toast.LENGTH_SHORT).show();
    }
}
}