Adarsh Adarsh - 2 months ago 10
Android Question

Service is being destroyed when activity is removed from background task android

I am starting a service class from my

mainActivity
like this:

Intent io = new Intent(MainActivity.this, Window.class);
startService(io);


It works perfectly on most of all devices but in some lenovo devices when I remove my app from background task the service is also destroyed with activity.I have tried
Sticky service
but it didn't worked.This type of issue is appearing on lenovo like devices only, on all most of other devices the service is not destroyed with activity and it is working fine.Can anyone help me to solve why is this happening.

I have also tried in this way but didn't worked out:

Intent io = new Intent(MainActivity.this, Window.class);
getBaseContext.startService(io);


My Manifest:

<?xml version="1.0" encoding="utf-8"?>




<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity"
android:launchMode="singleInstance">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<receiver android:name=".Boot" android:enabled="true" android:exported="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>

<service
android:name=".Capture"
android:label="@string/service_name"
android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE">
<intent-filter>
<action android:name="android.accessibilityservice.AccessibilityService" />
</intent-filter>

<meta-data
android:name="android.access"
android:resource="@xml/access" />
</service>

<activity android:name=".Faq"
android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen"
android:launchMode="singleInstance"/>

<service android:name=".Window"/>

<activity android:name=".Intro1"
android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen"/>
<activity android:name=".Intro2"
android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen"/>
<activity android:name=".Intro3"
android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen"/>
<activity android:name=".Permit"
android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen"/>


</application>



Answer

Had this problem too.

Most of Lenovo, Xiaomi, Asus, etc phones has Security system application in firmware which can block services. You should check it and unlock blocking of your app`s autostart if needed.

By default its turned off.

Also some BroadcastReceiver functions (like receiving of SMS) cannot work because of it even if you give required permission.

Worst thing is that you can`t check it programatically.