1

I'm trying to make an application that, on ring, will identify a phone number from an internet database. No matter what I do, MIUI (In Redmi Note 8T Android 11) eventually kills my application after a while and a notification of the phone state change is not received.

Following advice from here and from here and my manifest:

    <receiver android:name=".ServiceReceiver">
        <intent-filter>
            <action android:name="android.intent.action.PHONE_STATE" />
        </intent-filter>
    </receiver>

and the permissions:

<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_CALL_LOG" />
<uses-permission
    android:name="android.permission.WRITE_CALL_LOG"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_CONTACTS"/>

and in onCreate:

 ComponentName onBootReceiver = new ComponentName(getApplication().getPackageName(), BootReceiver.class.getName());
    if(getPackageManager().getComponentEnabledSetting(onBootReceiver) != PackageManager.COMPONENT_ENABLED_STATE_ENABLED)
        getPackageManager().setComponentEnabledSetting(onBootReceiver,PackageManager.COMPONENT_ENABLED_STATE_ENABLED,PackageManager.DONT_KILL_APP);

and my service receiver:

public void onReceive(Context context, Intent intent) {
    MyPhoneStateListener phoneListener = new MyPhoneStateListener();
    telephony = (TelephonyManager) context
            .getSystemService(Context.TELEPHONY_SERVICE);
    telephony.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE);
}

public void onDestroy() {
    telephony.listen(null, PhoneStateListener.LISTEN_NONE);
}

and my boot receiver:

@Override public void onReceive(Context context, Intent intent) {

    if (Objects.equals(intent.getAction(), Intent.ACTION_BOOT_COMPLETED)) {
        Intent i = new Intent(context, MainActivity.class);
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i);
    }
}

and also, in MIUI preferences, I 've set the app to 'auto start', remove all battery restrictions and enabled all manual permissions MIUI has in the preferences for this app.

What else can I do? Thanks a lot.

Michael Chourdakis
  • 10,345
  • 3
  • 42
  • 78

3 Answers3

1

You can employ these method(s) for avoid getting your application killed by MIUI:

- App pinning from the recents menu 
- Turn off MIUI Optimizations from developer options in settings
- Enable Autostart, Goto app settings and in battery saver > No restrictions, Allow access to all the required permissions (always)
- Change MIUI Power Management options 

Go through https://dontkillmyapp.com/xiaomi for more information on the same.

Mritunjay Gupta
  • 311
  • 2
  • 8
0

Have you tried the lock button in the app/task switcher ? You could also try the memory optimization settings 'Developer options' (if they are still present): https://s.kaskus.id/images/2018/06/11/521121_20180611073055.png

If nothing else works you could also add a background service to the app...

Crispert
  • 1,102
  • 7
  • 13
0

I had the similar problem on phones:

  • Redmi 7A with Android 10, MIUI Global 12.5.2 QCMRUXM
  • Redmi 4A with Android 7, MIUI 10.

MIUI kept closing Telegram-SMS background service when a phone is not connected to a charger.

Turning off MIUI Optimizations option did not help.

Turning off Power Detector app by:

adb shell
pm disable-user com.xiaomi.powerchecker # Power Detector
#Package com.xiaomi.powerchecker new state: disabled-user

also did not help.

The following advice helped to solve the problem:

Go to Security app > tap settings (gear on top right) > Boost speed > Lock apps > Lock the app you need to keep running. This replaces the "padlock" icon (App pinning from the recents menu) that no longer is visible in MIUI 12.

Source: Stop killing my apps Xiaomi!!


MIUI Optimization toggle is hidden in Developer options. To make it visible need to tap several times on Reset to default values option that almost at the bottom of Developers options. I did not notice that this option actually resets something. MIUI Optimization toggle will appear at the bottom of the list of options. Source


According to logcat log Telegram SMS app was being killed by Power Checker (when the app was not locked in recents menu):

01-06 10:50:15.716  4344  4652 W PowerChecker.Controller: autoKillApp, calling ProcessManager uid = 10289, pkg = com.termux
01-06 10:50:15.716  1580  2093 I ProcessManager: Kill reason AutoPowerKill from pid=4344
01-06 10:50:15.829  1580  2093 I ProcessManager: AutoPowerKill: kill com.termux Adj=200 State=5
01-06 10:50:15.830  1580  2093 I ActivityManager: Killing 13234:com.termux/u0a289 (adj 200): AutoPowerKill
01-06 10:50:15.875  4344  4652 W PowerChecker.Controller: autoKillApp, calling ProcessManager uid = 10305, pkg = com.qwe7002.telegram_sms
01-06 10:50:15.876  1580  2093 I ProcessManager: Kill reason AutoPowerKill from pid=4344
01-06 10:50:15.877  1580  2093 I ProcessManager: AutoPowerKill: kill com.qwe7002.telegram_sms:command Adj=200 State=5
01-06 10:50:15.878  1580  2093 I ActivityManager: Killing 23378:com.qwe7002.telegram_sms:command/u0a305 (adj 200): AutoPowerKill
01-06 10:50:15.880  1580  2093 I ProcessManager: AutoPowerKill: kill com.qwe7002.telegram_sms:battery Adj=200 State=5
01-06 10:50:15.880  1580  2093 I ActivityManager: Killing 23348:com.qwe7002.telegram_sms:battery/u0a305 (adj 200): AutoPowerKill
01-06 10:50:15.961  1580  1848 I ProcessManager: remove no clear notification:StatusBarNotification(pkg=com.termux user=UserHandle{0} id=1337 tag=null key=0|com.termux|1337|null|10289: Notification(channel=termux_notification_channel pri=1 contentView=null vibrate=null sound=null defaults=0x0 flags=0x62 color=0xff607d8b actions=2 vis=PRIVATE))
01-06 10:50:15.968  1580  3450 W ActivityManager: Scheduling restart of crashed service com.termux/.app.TermuxService in 1000ms
01-06 10:50:15.973  1580  1665 W ActivityManager: setHasOverlayUi called on unknown pid: 13234
01-06 10:50:15.977  1580  4023 W ActivityManager: Scheduling restart of crashed service com.qwe7002.telegram_sms/.battery_service in 10992ms
01-06 10:50:15.983  1580  1848 I ProcessManager: remove no clear notification:StatusBarNotification(pkg=com.qwe7002.telegram_sms user=UserHandle{0} id=2 tag=null key=0|com.qwe7002.telegram_sms|2|null|10305: Notification(channel=Chat command  pri=0 contentView=null vibrate=null sound=null tick defaults=0x0 flags=0x62 color=0x00000000 vis=PRIVATE))
01-06 10:50:15.984  1580  4019 W ActivityManager: Scheduling restart of crashed service com.qwe7002.telegram_sms/.chat_command_service in 20984ms
01-06 10:50:16.650 23510 23510 E ExtAssistant: Error occurred processing removal of StatusBarNotification(pkg=com.termux user=UserHandle{0} id=1337 tag=null key=0|com.termux|1337|null|10289: Notification(channel=termux_notification_channel pri=1 contentView=null vibrate=null sound=null defaults=0x0 flags=0x62 color=0xff607d8b actions=2 vis=PRIVATE))
Anton Samokat
  • 542
  • 1
  • 6
  • 15