android - 有什么方法可以从 android 上收到的短信中获取发件人电话号码?

标签 android android-emulator sms android-sdk-2.1

我成功地编写了发送和接收短信程序,但我想在 onReceive 方法中检查接收者的电话号码。我如何从android中收到的短信中获取发件人电话号码? 我写了这段代码,但它确实有效!!!请检查并帮助我。

public class SmsReceiver extends BroadcastReceiver {
    public String str = "";

    @Override
    public void onReceive(Context context, Intent intent) {
        // ---get the SMS message passed in---

        Bundle bundle = intent.getExtras();
        SmsMessage[] msgs = null;

        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]);

                //for get sms from special number===============================
                String msg_from = msgs[i].getOriginatingAddress();
                Log.v("msg_from >>",msg_from);     
                if(msg_from.equals("08522215"))
                {
                    //===============================
                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();
            Intent act = new Intent(context, MainActivity.class);
            act.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            act.putExtra("message", str);
            context.startActivity(act);
        }

        abortBroadcast();
        }
    }

list .xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.sms"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="7"
        android:targetSdkVersion="15" />

    <uses-permission android:name="android.permission.SEND_SMS">
    </uses-permission>
    <uses-permission android:name="android.permission.RECEIVE_SMS">
    </uses-permission>

<uses-permission android:name="android.permission.READ_SMS" />
    <application android:icon="@drawable/ic_launcher" android:label="@string/app_name">
        <activity android:name=".SMS"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity android:name=".MainActivity"
                  android:label="@string/app_name"/>
       <receiver
            android:name="com.example.sms.SmsReceiver"
            class="com.example.sms.SmsReceiver" >
            <intent-filter android:priority="100" >
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            </intent-filter>
        </receiver>


    </application>


</manifest>

最佳答案

在 bundle 对象本身中接收发件人编号。

关于android - 有什么方法可以从 android 上收到的短信中获取发件人电话号码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13098472/

相关文章:

Android 标准和 STL 支持

android - 从android访问asp.net web api http服务

android-studio - Android studio 4.1 - 如何禁用直接在 Android Studio 中运行 Android 模拟器

Android:如果我使用 SmsManager 发送短信,它会自动重新发送失败的短信吗?

android - ConstraintLayout 在彼此之间居中 View

java - 使用 URL 设置图像失败

android - Android Studio 中的模拟器启动但未打开

android - 发布前报告 "Issue: native crash"

delphi - 当我对 SMS PDU(GSM 7 位)用户数据进行编码/解码时,是否需要先添加 UDH?

iphone - iPhone 上的二进制短信