2

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());
                }
            }
        }
    }
}
Dimitar Vukman
  • 421
  • 6
  • 15
  • Which Java code are you using to catch the SMS ? – user3793589 Jun 20 '16 at 17:05
  • Added java code. It is not cleaned up, but it does not matter for this problem :-). – Dimitar Vukman Jun 20 '16 at 17:17
  • 1
    Provided your ``'s `name` is pointing to the right class file, and you've run your app at least once after installation, I think your issue might be with your device. I believe Xiaomis disallow third-party apps to receive SMS by default. Check the Settings for your app. Also, you don't need the `protectionLevel` attribute on the permission, though I don't believe that is causing the problem. – Mike M. Jun 20 '16 at 23:25
  • Thanks Mike. I believe protection level is for Android 6, but I had to try adding it. I will check for settings. – Dimitar Vukman Jun 21 '16 at 09:14
  • 1
    Isuse seems to be resolved. Xiaomi has different permissions from what I have seen in other phones. This is what I had to do: go to settings, installed apps, find my test app, tap it, go to permissions manager and enable sms reading settings... i just enabled all . Then had to close app, clear cache (optional probably) and start it again. Event passed nicely this time. Problem resolved. – Dimitar Vukman Jun 21 '16 at 09:43
  • Would you mind editing your answer below to add the steps you outlined in your comment? I'd like to use this post as a reference for others with a similar issue, and having that in the answer would be better than trying to direct them to a comment. Thanks a lot! – Mike M. Apr 26 '17 at 23:24

1 Answers1

3

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
Community
  • 1
  • 1
Dimitar Vukman
  • 421
  • 6
  • 15
  • I have problem with android 8 xamoi phones with RECEIVE_SMS when app is in background and Receiver is triggred but message is empty. Do have any idea about this? – Mahdi Apr 20 '19 at 10:56