Adarsh
Adarsh

Reputation: 185

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>

Upvotes: 2

Views: 2055

Answers (3)

Mihai Neacsu
Mihai Neacsu

Reputation: 245

You might want to take a look at this :

How to keep a service running in background even after user quits the app?

and developer.android.com :

Running a Service in the Foreground A foreground service is a service that's considered to be something the user is actively aware of and thus not a candidate for the system to kill when low on memory. A foreground service must provide a notification for the status bar, which is placed under the "Ongoing" heading, which means that the notification cannot be dismissed unless the service is either stopped or removed from the foreground.

Notification notification = new Notification(R.drawable.icon, getText(R.string.ticker_text),
        System.currentTimeMillis());
Intent notificationIntent = new Intent(this, ExampleActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(this, getText(R.string.notification_title),
        getText(R.string.notification_message), pendingIntent);
startForeground(ONGOING_NOTIFICATION_ID, notification);

Also, you can try to add to your onDestroy() function, a alarm that will restart the service after a few seconds since when it was destroyed.

Upvotes: 0

Andrey Danilov
Andrey Danilov

Reputation: 6592

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.

Upvotes: 1

Farhad
Farhad

Reputation: 12976

You can run your service in a separate process. Add the following attribute to the service declaration in the AndroidManifest

android:process=":any-name-for-process"

Upvotes: 0

Related Questions