广播接收器未被调用

6

我正在尝试构建一个简单的应用程序,它将等待新的短信到来,从中提取和处理数据。该应用程序应在后台运行。GUI具有单个切换元素以启动/停止广播接收器。即使应用程序被销毁并且屏幕被锁定,除非用户手动关闭它,否则应用程序仍然可以工作。

我搜索了stackoverflow上的每一个资源,大多数人都是这样做的,但对我而言仍然不起作用,我似乎不知道为什么。我知道是因为Log.d(...)没有返回任何内容。任何帮助都将不胜感激。

进入图像描述

广播短信接收器

public class SMSReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d("RECEIVER", "ENTERED");

        if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")){
            Log.d("RECEIVER", "SMS RECEIVED");

            SmsMessage[] messages = Telephony.Sms.Intents.getMessagesFromIntent(intent);
            for(int i = 0; i < 4; i++){
                SmsMessage sms = messages[i];
                Log.d("Message " + i + 1 + " from: ", sms.getOriginatingAddress());
                Toast.makeText(context,"SMS from " + sms.getOriginatingAddress(), Toast.LENGTH_SHORT).show();
            }
        }

    }
}

主要活动

public class MainActivity extends AppCompatActivity {
    IntentFilter filter;
    SMSReceiver receiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        filter = new IntentFilter();
        filter.addAction("android.provider.Telephony.SMS_RECEIVED");
        receiver = new SMSReceiver();

        Switch startSwitch = (Switch) findViewById(R.id.startSwitch);
        startSwitch.setChecked(false);

        startSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                if(isChecked){
                    getApplication().registerReceiver(new SMSReceiver(), filter);
                } else {
                    getApplication().unregisterReceiver(receiver);
                }
            }
        });

    }
}

清单

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.ahmad.smsforwarder">

    <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">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>

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

</manifest>

2
你的接收器在清单上在哪里? - AGM Tazim
3个回答

7

我曾经遭受太长时间的煎熬,才发现从Android 6.0(API 23)开始,您还需要在主活动java类中注册权限。 我通过将以下行添加到主活动类的onCreate()方法中来解决问题:

ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.READ_SMS},1);
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.RECEIVE_SMS},1);

1
请注意,如果这两行代码同时请求,它们将无法正常工作。您必须逐个请求它们。它们会导致一个日志记录语句:“2020-04-17 17:11:41.286 10156-10156/com.example.myapplication W/Activity: Can request only one set of permissions at a time”,但从用户界面方面来看,这将是一个静默的错误。 - Calicoder

6

您需要在AndroidManifest.xml中使用特定的意图过滤器来注册您的接收器。

    <receiver
        android:name=".SMSReceiver"
        android:enabled="true"
        android:exported="true">
        <intent-filter>
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />
        </intent-filter>
    </receiver>

0
请使用smsRetrieverAPI(消息模板需要在结尾处添加应用程序哈希字符串)或smsUserConsentAPI(消息模板不需要在结尾处添加应用程序哈希字符串)。这些Google API不再需要SMS读取权限。
Sms Retriever API https://developers.google.com/identity/sms-retriever/request Sms User Consent API https://developers.google.com/identity/sms-retriever/user-consent/overview
<receiver android:name=".MySMSBroadcastReceiver" android:exported="true"
          android:permission="com.google.android.gms.auth.api.phone.permission.SEND">
    <intent-filter>
        <action android:name="com.google.android.gms.auth.api.phone.SMS_RETRIEVED"/>
    </intent-filter>
</receiver>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接