简体   繁体   中英

Not Receiving SMS using SmsManager in Android

I have a broadcast receiver class for receiving sms

package package name;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Telephony;

import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;

public class IncomingSms extends BroadcastReceiver {

    // Get the object of SmsManager
    final SmsManager sms = SmsManager.getDefault();

    public static final String ACTION = "android.provider.Telephony.SMS_RECEIVED";

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        if (intent.getAction().equals(ACTION)){ 
            Bundle bundle = intent.getExtras();
            if (bundle != null){

                final Object[] pdusObj = (Object[]) bundle.get("pdus");

                for (int i = 0; i < pdusObj.length; i++) {

                    SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
                    String phoneNumber = currentMessage.getDisplayOriginatingAddress();

                    String senderNum = phoneNumber;
                    String message = currentMessage.getDisplayMessageBody();

                    Log.i("SmsReceiver", "senderNum: "+ senderNum + "; message: " + message);


                   // Show Alert
                    int duration = Toast.LENGTH_LONG;
                    Toast toast = Toast.makeText(context, 
                                 "senderNum: "+ senderNum + ", message: " + message, duration);
                    toast.show();

                } // end for loop
              } // bundle is null


    }    
        }
}

and this is my manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="data.mobile.balance"
    android:versionCode="3"
    android:versionName="1.03" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

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

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

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/icon"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="PACKAGE 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=".IncomingSms"> 
        <intent-filter android:priority="1000">
            <action android:name="android.intent.action.PHONE_STATE" />
            <action android:name="PACKAGE NAME.android.action.broadcast"/> 
             <action android:name="android.provider.Telephony.SMS_RECEIVED"></action>
        </intent-filter> 
    </receiver>

    </application>

</manifest>

I spent entire day running this code but still no success. What is wrong with my code ?

Do I have to register the broadcast receiver in my main activity ?

UPDATED Tested on 5.0.1

I made a simple demo & it's work like charm.

Manifest.xml

<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

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

Receiver:

public class SmsReciever extends BroadcastReceiver {
public SmsReciever() {
}

@Override
public void onReceive(Context context, Intent intent) {
    Toast.makeText(context,"sms Received",Toast.LENGTH_SHORT).show();

    Bundle bundle = intent.getExtras();
    SmsMessage[] msgs = null;
    String messageReceived = "";
    if (bundle != null)
    {
        //---retrieve the SMS message received---
        Object[] pdus = (Object[]) bundle.get("pdus");
        msgs = new SmsMessage[pdus.length];
        for (int i=0; i<msgs.length; i++)
        {
            msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
            messageReceived += msgs[i].getMessageBody().toString();
            messageReceived += "\n";
        }
        //---display the new SMS message---
        Toast.makeText(context, messageReceived, Toast.LENGTH_SHORT).show();
        // Get the Sender Phone Number
        String senderPhoneNumber=msgs[0].getOriginatingAddress ();

    }
}

}

I'm not sure if it has anything to do with your problem but have you tried removing / reducing the 'priority' value of the receiver? According to google documentation it must be an integer greater than -1000 and less than 1000...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM