Android - Broadcast Receiver not being fired

南笙酒味 提交于 2019-12-01 10:28:42

问题


I know this has been asked ALOT on here, but I have been scouring the interwebs for hours and I have even reused some of my previous code for receiving sms' and I got...nothing.

So, here goes, basic app to receive SMS but the app never receives the intent. I thought the intent may be ignored if the text is sent from the same phone but that does not seem to be the case, as other apps pick up the text fine.

Here is my manifest:

   <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.encima.smsreceiver"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".MainActivity" android:label="@string/app_name">
                        <intent-filter>
                                <action android:name="android.intent.action.MAIN" />
                                <category android:name="android.intent.category.LAUNCHER" />
                        </intent-filter>
         </activity>
         <receiver android:name=".MessageReceiver">
            <intent-filter>
                <action android:name="android.provider.Telephony.SMS_RECEIVED"></action>
            </intent-filter>
        </receiver>
    </application>
    <uses-sdk android:minSdkVersion="8" />
    <uses-permission android:name="android.permission.RECEIVE_SMS" />
</manifest>

And, here is the receiver, nothing seems to be new here, so I have no idea what the problem is:

package com.encima.smsreceiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;

public class MessageReceiver extends BroadcastReceiver {

    private static final String TAG = "Message recieved";
    @Override
    public void onReceive(Context context, Intent intent) {
         Bundle pudsBundle = intent.getExtras();
         Object[] pdus = (Object[]) pudsBundle.get("pdus");
         SmsMessage messages =SmsMessage.createFromPdu((byte[]) pdus[0]);    
         Log.i(TAG,  messages.getMessageBody());
         Toast.makeText(context, "SMS Received : "+messages.getMessageBody(),
         Toast.LENGTH_LONG).show();
    }

}

The debug phone I am using is running 2.2.2 and I have other apps running that detect sms, including some of my own.

Any insight into this would be appreciated!

Thanks


回答1:


Because the SMS broadcast intent is sent by Context.sendOrderedBroadcast(...), if any other app registers the BroadcastReceiver and calls abortBroadcast, the other receiver will not get the broadcast.

To increase the probability of your app receiving the broadcast create an IntentFilter, use IntentFilter.setPriority.




回答2:


I do not know if this is your problem but you should definitelly try this:

Instead of ".MessageReceiver" put android:name = "com.encima.smsreceiver.MessageReceiver"

This is fix that workout many times for me when something doesn't get called.



来源:https://stackoverflow.com/questions/6966902/android-broadcast-receiver-not-being-fired

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!