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

和自甴很熟 提交于 2019-12-22 12:24:08

问题


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());
    }
 }
}

回答1:


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




回答2:


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.

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); } }



来源:https://stackoverflow.com/questions/41506105/sms-broadcastreceiver-doesnt-receive-sms-after-app-killed-or-device-restart-on

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!