1

I have an application with a service. Service is started by an app in the main activity on create method. My problem occurs when I exit from the application: service stops

Manifest File

<application
    android:name=".App"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:isolatedProcess="true"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".SplashActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".DialActivity">
    </activity>
    <service
        android:name=".ButtDial.CallDetectService"
        android:enabled="true"
        android:exported="true" />

    <activity android:name=".Utils.IncomingCallActivity" />
    <activity android:name=".Utils.OutGoingCallActivity"></activity>
    <receiver
        android:name=".ButtDial.CallHelper$OutgoingReceiver"
        android:enabled="true"
        android:exported="true"
        android:label="RestartServiceWhenStopped">

    </receiver>
</application>

I don't know how to initiate my service when an app exit Service

public class CallDetectService extends Service {
private CallHelper callHelper;

public CallDetectService() {
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    callHelper = new CallHelper(this);

    int res = super.onStartCommand(intent, flags, startId);
    callHelper.start();
    return START_STICKY;
}

@Override
public void onDestroy() {
    super.onDestroy();

    callHelper.stop();
}
@Override
public IBinder onBind(Intent intent) {
    // not supporting binding
    return null;}
public class MyBinder extends Binder {
    public CallDetectService getService() {
        return CallDetectService.this;
    }
}}

In Activity

Please help me for this issue

Intent intent = new Intent(getBaseContext(), CallDetectService.class);
startService(intent);
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
kirubha sankar
  • 150
  • 1
  • 12

2 Answers2

0

You can use android:stopWithTask="false" in manifest as bellow, This means even if user kills app by removing it from tasklist, your service won't stop.

 <service
   android:name="CallDetectService "
   android:stopWithTask="false"
   android:isolatedProcess="true"/>

for more have look this. Hope it will help you!!

Hemant Parmar
  • 3,924
  • 7
  • 25
  • 49
0

Even if you manage to find some way to execute on Android N, the approach won't work from Android O which has imposed several restriction on background services. You won't be able to freely run or start the Services. When app enters background, Service will be stopped as if stopSelf() is called.

If its crucial for you to ensure Service runs to completion, even if app is closed, then you can create ForegroundService. This approach will also work on Android O. You can follow my answer on this SO for implementation details.

Sagar
  • 23,903
  • 4
  • 62
  • 62
  • Thanks, Kindly say where to place foreground in Service Class @Sagar – kirubha sankar Jul 18 '18 at 09:45
  • @kirubhasankar you need to call it in `onCreate()`. Create notification and when you start the service, `ContextCompat.startForegroundService(context, serviceIntent)` – Sagar Jul 18 '18 at 10:02