sms BroadcastReceiver doesn't receive SMS after app killed OR device restart on MI devices only

1.3k Views Asked by At

My SMS receiving code works good on all devices except Xiomi Redmi devices

On Xiomi Redmi devices, my app(Broadcast Receiver) not able to receive SMS when app gets killed by swiping from recent app list OR after device restart until I start the app manually. (Tested on Mi Marshmallow and MI Lollipop devices).

This issue happens only on MI devices. App works good on other devices like Samsung, HTC, Sony, Motorola, Micromax etc.

my code in manifest:

 <uses-permission android:name="android.permission.SEND_SMS" />
 <uses-permission android:name="android.permission.RECEIVE_SMS" />
 <receiver
        android:name=".receiver.SMSBroadcastReceiver"
        android:enabled="true"
        android:priority="999">
        <intent-filter>
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />
        </intent-filter>
  </receiver>

Broadcast Receiver:

public class SMSBroadcastReceiver extends BroadcastReceiver {

 public void onReceive(Context context, Intent intent) {
    if (Constants.SMS_RECEIVED_ACTION.equals(intent.getAction())) {
        mContext = context.getApplicationContext();
        mIntent = intent;
        sendIntent();
    } else {
        LogUtil.e(TAG, "Intent action: " + intent.getAction());
    }
 }
}
2

There are 2 best solutions below

2
On

Xiomi Redmi devices, app restrictions are more stringent than any other ROM, due to these restrictions many of the app notifications are not meeting the timeline and delivers the notifications only after some certain period of time. The reasons may be of different ranges, starting from Google's cloud messaging to advanced battery modes, that enable apps to sleep instead of getting the notifications.To Resolve this issue Read this document carefully http://en.miui.com/forum.php?mod=viewthread&tid=268224&page=1

0
On

Finally got the solution(workaround actually)

1. First check if MI device

if (!TextUtils.isEmpty(getMiUiVersionProperty())) { // its a MI device }

public String getMiUiVersionProperty() {
    BufferedReader reader = null;
    try {
        reader = new BufferedReader(new InputStreamReader(Runtime.getRuntime().exec("getprop ro.miui.ui.version.name").getInputStream()), 1024);
        String line = reader.readLine();
        reader.close();
        return line;
    } catch (IOException e) {}
}

2. Show a dialog to user to enable Autostartfor your application e.g. enter image description here

3. Then navigate user to 'Autostart' screen directly to enable it for your app

public void openMiuiAutoStartPermissionActivity(Context context) { Intent intent = new Intent("miui.intent.action.APP_PERM_EDITOR"); String ROM = getMiUiVersionProperty(); if (TextUtils.equals(ROM, "V5")) { PackageInfo pInfo = null; try { pInfo = context.getPackageManager().getPackageInfo( context.getPackageName(), 0); } catch (NameNotFoundException e) { e.printStackTrace(); } intent.setClassName("com.android.settings", "com.miui.securitycenter. permission.AppPermissionsEditor"); intent.putExtra("extra_package_uid", pInfo.applicationInfo.uid); } else if (TextUtils.equals(ROM, "V6") || TextUtils.equals(ROM, "V7") || TextUtils.equals(ROM, "V8")) { intent.setClassName( "com.miui.securitycenter", "com.miui.permcenter.autostart.AutoStartManagementActivity"); intent.putExtra( "extra_pkgname", context.getPackageName()); } else { intent.setClassName( "com.miui.securitycenter", "com.miui.permcenter.autostart.AutoStartManagementActivity"); intent.putExtra( "extra_pkgname", context.getPackageName()); } if (isIntentAvailable(context, intent) && (context instanceof Activity)) { ((Activity) context).startActivityForResult(intent, AUTO_START_ENABLE_REQUEST); } }