Android 5.0.2 - SMS Broadcast Receiver - can not get it working

I am testing on Xiaomi Redmi Note 3 and what I need is very simple: * Register broadcast receiver for incoming text messages * Once message comes in, just read it

It looks like I can not get receiver register no matter what I try.

From the google docs, since 4.4 there should be no way for any app to swallow the event and every app listening should get a chance to get the event.

I have tried all kind of combinations and googled pretty much everything. Could it be the Xiaomi phone issue?

Here is my manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.com.dimitar.test"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="19"
        android:targetSdkVersion="21" />

    <uses-permission
        android:name="android.permission.RECEIVE_SMS"
        android:protectionLevel="dangerous" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <receiver
            android:name="com.example.com.dimitar.test.SmsListener"
            android:enabled="true" >
            <intent-filter>
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            </intent-filter>
        </receiver>

        <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>
    </application>

</manifest>

Java code:

 package com.example.com.dimitar.test;

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

public class SmsListener extends BroadcastReceiver{

    private SharedPreferences preferences;

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

         Toast toast = Toast.makeText(context, "poruka: ", Toast.LENGTH_SHORT);
        toast.show();

        if(bundle != null){
            //---get the SMS message passed in---
            SmsMessage[] msgs = null;
            String msg_from;
            if (bundle != null){
                //---retrieve the SMS message received---
                try{
                    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]);
                        msg_from = msgs[i].getOriginatingAddress();
                        String msgBody = msgs[i].getMessageBody();


                        Toast toast1 = Toast.makeText(context, "poruka: " + msgBody, Toast.LENGTH_SHORT);
                        toast1.show();
                    }
                }catch(Exception e){
//                            Log.d("Exception caught",e.getMessage());
                }
            }
        }
    }
}

Solution 1:

Looks like Xiaomi has a Security application that controls pretty much everything. See another question and answer here

Steps:

  • go to settings > installed apps
  • find the app > tap it
  • go to permissions manager and enable permission you need

Or:

  • go to Security app
  • tap Permissions
  • Choose Autostart or Permissions and enable whatever you need for your app