我想从我们的应用程序发送和接收短信并发送成功,但接收显示我的代码的SmsMessage类中的弃用,这是由我的网站在网站上演示的。所以,我想要一个不被弃用的SMS接收方法。请建议我找到合适的解决方案。
接收代码:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.gsm.SmsMessage;
import android.widget.Toast;
public class SmsReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
//---get the SMS message passed in---
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str = "";
if (bundle != null)
{
//---retrieve the SMS message received---
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]);
str += "SMS from " + msgs[i].getOriginatingAddress();
str += " :";
str += msgs[i].getMessageBody().toString();
str += "\n";
}
//---display the new SMS message---
Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
}
}
}
答案 0 :(得分:3)
import android.telephony.gsm.SmsMessage;
以上内容应改为:
import android.telephony.SmsMessage;
答案 1 :(得分:0)
将 BROADCAST_SMS 权限添加到您的接收器并启用并导出为 true ,将在沙拉赫有效
<receiver android:name="com.mohamedtest.sendandreceivesms_m.SMSReceiver"
android:enabled="true"
android:exported="true"
android:permission="android.permission.BROADCAST_SMS">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>