SMS_RECEIVED onReceive android 2.3.5 not triggering

99封情书 提交于 2019-12-09 06:57:26

The main problem is that this line in "mysmstestcall" is wrong:

if (intent.getAction() == SMS_RECEIVED)

should be changed to this:

if (intent.getAction().equals(SMS_RECEIVED)) 

Is there a reason that you have two recievers? You have a programatic listener and you have an XML listener

Programatic:

filter.addAction("android.provider.Telephony.SMS_RECEIVED");
Log.i(TAG, " Filter SMS_RECEIVED has been added");
//Extends BroadcastReceiver
receiver = new mysmstestcall();

XML Listener:

<receiver android:name=".mysmstestcall">
<intent-filter>
    <action android:name="android.provider.Telephony.SMS_RECEIVED"></action>
</intent-filter>

Are you sure you want these two? If you have two you will RECEIVE the same broadcast 2 times...

As for another ISSUE that I see is this line

if (intent.getAction() == SMS_RECEIVED)

When comparing strings you DO NOT compare them like this instead you'd have something like this:

if (intent.getAction().equalsIgnoreCase(SMS_RECEIVED))

I've got this once. It can be network's issue. These were my question and answer. You can add a sending intent and catch the result code. In my case, it was RESULT_ERROR_GENERIC_FAILURE. I tried to find other solutions for months but no luck, so I accepted that even though I don't want to :-(

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

public class SmsBroadcastReceiver extends BroadcastReceiver{
    public static final String SMS_BUNDLE = "pdus";
    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle intentExtras = intent.getExtras();
        if (intentExtras != null) {
            Object[] sms = (Object[]) intentExtras.get(SMS_BUNDLE);
            String smsMessageStr = "";
            for (int i = 0; i < sms.length; ++i) {
                SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) sms[i]);

                String smsBody = smsMessage.getMessageBody().toString();
                String address = smsMessage.getOriginatingAddress();

                smsMessageStr += "SMS From: " + address + "\n";
                smsMessageStr += smsBody + "\n";
            }
            Toast.makeText(context, "A new message is added to the SMS List!!!\n"+smsMessageStr, Toast.LENGTH_SHORT).show();

            //this will update the UI with message
            InboxMain inst = InboxMain.instance();
            inst.updateList(smsMessageStr);

    }

}
}


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

permission::
<uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.WRITE_SMS" />
    <uses-permission android:name="android.permission.READ_SMS" />
    <uses-permission android:name="android.permission.RECEIVE_SMS" />
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!