【问题标题】:onReceive() is not triggered with incoming SMSonReceive() 不会被传入的 SMS 触发
【发布时间】:2025-11-30 05:55:02
【问题描述】:

我试图读取传入的短信并根据短信中的文本执行一些操作,但未触发 onReceive() 方法。

清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.dost.anshdeep.dosti">
    <uses-permission android:name="android.permission.RECEIVE_SMS" />
    <uses-permission android:name="android.permission.READ_SMS" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Ringing"
            android:label="@string/title_activity_ringing"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="com.dost.anshdeep.dosti.Ringing" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Alarms"
            android:label="@string/title_activity_alarms"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="com.dost.anshdeep.dosti.Alarms" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <receiver android:name=".SmsListener">
            <intent-filter>
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            </intent-filter>
        </receiver>

    </application>

</manifest>

SmsListener.java

 public class SmsListener extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context,"Hello",Toast.LENGTH_LONG).show();
        //Log.d("Message Received : ",msgBody);
        if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")){
            Bundle bundle = intent.getExtras();           //---get the SMS message passed in---
            SmsMessage[] msgs = null;
            if (bundle != null){
                //---retrieve the SMS message received---
                try{
                    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]);
                        String msg_from = msgs[i].getOriginatingAddress();
                        String msgBody = msgs[i].getMessageBody();
                    }
                }catch(Exception e){
                            Log.d("Exception caught", e.getMessage());
                }
            }
        }
        //Toast.makeText(context,msg_from,Toast.LENGTH_LONG);
    }
}

我能知道这段代码有什么问题吗,因为我不知道为什么没有调用 onReceive()。

【问题讨论】:

  • 非常感谢您的帮助 Arshad,但它对我不起作用。我想知道我的代码不起作用的原因。仍然没有调用 onReceive()。任何帮助将不胜感激。

标签: android broadcastreceiver


【解决方案1】:

在 API 23 及更高版本上,您还应该获得在运行时接收 SMS 的权限。看here

【讨论】:

    【解决方案2】:

    尝试在 Manifest 中像这样声明您的 BroadcastReceiver

    <receiver
            android:name=".SmsReceiver"
            android:exported="true"
            android:permission="android.permission.BROADCAST_SMS">
    
            <intent-filter android:priority="2332412">
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            </intent-filter>
    
        </receiver>
    

    和您的广播接收器

    public class SmsReceiver extends BroadcastReceiver {
    
    private static final String TAG = SmsReceiver.class.getSimpleName();
    
    @Override
    public void onReceive(Context context, Intent intent) {
    
        Log.e(TAG, "Checking Sms");
        Bundle bundle = intent.getExtras();
        try{
            if(bundle != null){
                Object[] pdusObj = (Object[]) bundle.get("pdus");
                for (Object aPdusObj : pdusObj) {
                    SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) aPdusObj);
                    String senderAddress = currentMessage.getDisplayOriginatingAddress();
                    String message = currentMessage.getDisplayMessageBody();
    
                    Log.e(TAG, "Received SMS: " + message + ", Sender: " + senderAddress);
    
                    if (!senderAddress.toLowerCase().contains(GlobalFunctions.SMS_ORIGIN.toLowerCase())) {
                        return;
                    }
    
                    // verification code from sms
                    String verificationCode = getVerificationCode(message);
    
                    Log.e(TAG, "OTP received: " + verificationCode);
    
                    if(AppController.isActivityVisible()) {
                        Intent smsService = new Intent(context, SmsService.class);
                        smsService.putExtra("otp", verificationCode);
                        context.startService(smsService);
                    }
                }
            }
        } catch (Exception e){
            Log.e(TAG, "Exception");
            e.printStackTrace();
        }
    
    }
    

    }

    注意:不要复制整个代码,它包含我声明的变量。只需按照代码并根据需要编写自己的代码

    【讨论】:

    • 请注意,如果另一个应用程序声明一个广播接收器以更高的优先级侦听相同的意图并且不传递意图,那么所有低优先级的广播接收器将不会接收到意图。 Handcent SMS 可以做到这一点。
    最近更新 更多