未调用广播接收器

Broadcast Receiver not being called

我正在尝试构建一个简单的应用程序,它会等到新的 SMS 到达,然后从中提取和处理数据。该应用程序应 运行 在后台运行。 GUI 有一个开关元素到 start/stop 广播接收器。即使应用程序被销毁并且屏幕被锁定,应用程序应该仍然可以工作,除非用户手动将其关闭。 我在 Whosebug 上搜索了所有资源,其中大部分都是这样做的,但是,它仍然对我不起作用,我似乎不知道为什么。我知道,因为 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();
            }
        }

    }
}

主要Activity

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>

您必须在 AndroidManifest.xml 中使用 intent 过滤器中的特定 intent 注册您的接收器。

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

我苦恼了太久才发现,从 Android 6.0 (API 23) 开始,您将必须在主 activity java [ 中注册权限=18=] 还有。我通过将以下几行添加到主要 activity 的 class onCreate() 方法来解决问题:

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