javaandroidbroadcastreceiversmssmsmanager

Broadcast Receiver Not receive messages from Mobile default messags application Why?


In BroadcastReceiver i want to receive sms from default Mobile application.

But when user send sms without internet connect then i receive, but when user (send sms via wifi or Mobile Network) i unable to read message.

public class MessageReciver extends BroadcastReceiver {

private static MessageListener mListener;
public static final String reciveSMS="android.provider.Telephony.SMS_RECEIVED";
public static final String TAG ="SmsBroadcastReceiver";
@Override
public void onReceive(Context context, Intent intent) {
    Log.i("fsdfdsfdsfdgfdsg  ","  rerwerw");
    if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")) {
        Bundle data = intent.getExtras();
        Object[] pdus = (Object[]) data.get("pdus");
        String formate = data.getString("format");

        for (int i = 0; i < pdus.length; i++) {
            SmsMessage smsMessage = null;
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) 
             {
                smsMessage = SmsMessage.createFromPdu((byte[]) pdus[i], formate);
            } else {
                smsMessage = SmsMessage.createFromPdu((byte[]) pdus[i]);
            }
            String message = smsMessage.getMessageBody();
            Toast.makeText(context, "Message Received: " + message, Toast.LENGTH_SHORT).show();
        }
    }
}

}

I have't understand why i face this problem , because Broadcast receiver can send or receive broadcast messages from the Android system and other Android apps. It only receive (sms without using internet), But every mobile messages application have default enable option(Use wifi or data for messaging when available).

Anyone please help receive sms from default Mobile Messages Application


Solution

  • already register your receiver yet?

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        registerReceiver(messageReciver , new  IntentFilter(YOUR FILTER));
    }
    

    try this

    AndroidManifest.xml

    <uses-permission android:name="android.permission.RECEIVE_SMS"/>
    
    <application>
        // add this
        <receiver android:name=".MessageReciver">
            <intent-filter android:priority="1">
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            </intent-filter>
         </receiver>
    </application>
    

    MainActivity.java

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        if ( ContextCompat.checkSelfPermission(
                this,
                Manifest.permission.RECEIVE_SMS
        ) != PackageManager.PERMISSION_GRANTED
        ) {
            ActivityCompat.requestPermissions(this, new String[]{ Manifest.permission.RECEIVE_SMS}, 1);
        }
    }