广播接收器未被呼叫

时间:2017-03-04 17:54:01

标签: android broadcastreceiver sms

我正在尝试构建一个简单的应用程序,它会等到新的SMS到达,从中提取并处理数据。应用程序应该在后台运行。 GUI具有单个开关元件以启动/停止广播接收器。即使应用程序被销毁并且屏幕被锁定,应用程序仍应正常工作,除非用户手动将其关闭。 我在stackoverflow上搜索了每一个资源,其中大多数都是这样做的,然而,它仍然不适合我,我似乎不知道为什么。我知道因为Log.d(...)没有返回任何东西。任何帮助将不胜感激。

enter image description here

广播SMS接收器

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 个答案:

答案 0 :(得分: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>

答案 1 :(得分:5)

我发现从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);