如何从android应用程序接收短信?
我想从我们的应用程序发送和接收短信,发送成功,但接收显示在我的代码的SmsMessage类中的弃用,这是由我的网站在web上演示的。所以,我想要一个短信接收的接收方法,这不是不推荐的。请给我建议正确的解决方案 接收代码:如何从android应用程序接收短信?,android,Android,我想从我们的应用程序发送和接收短信,发送成功,但接收显示在我的代码的SmsMessage类中的弃用,这是由我的网站在web上演示的。所以,我想要一个短信接收的接收方法,这不是不推荐的。请给我建议正确的解决方案 接收代码: import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.os.Bundle; import a
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();
}
}
}
导入android.content.BroadcastReceiver;
导入android.content.Context;
导入android.content.Intent;
导入android.os.Bundle;
导入android.telephony.gsm.sms消息;
导入android.widget.Toast;
公共类SmsReceiver扩展了BroadcastReceiver
{
@凌驾
公共void onReceive(上下文、意图)
{
//---获取传入的SMS消息---
Bundle=intent.getExtras();
SmsMessage[]msgs=null;
字符串str=“”;
if(bundle!=null)
{
//---检索收到的SMS消息---
Object[]pdus=(Object[])bundle.get(“pdus”);
msgs=新SMS消息[PDU.length];
对于(int i=0;i导入android.telephony.gsm.SmsMessage;
上述内容应更改为:
导入android.telephony.SmsMessage;
向您的接收器添加广播\u SMS权限,并使启用和导出为真,将在HALLAH中工作
仅供参考,telephony.gsm.SmsMessage
用于您必须在CDMA和gsm手机之间进行指定的时候。现在您不需要指定.gsm
或.CDMA
,因为Android变得更智能了。我在这里也做了类似的事情!!!
<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>