0

I'm using the foreground service with notification. I remove the service in the main activity onDestroy method by calling stopForeground and stopService. The problem is when I swipe my app of the recent apps to kill it, the debug session is still running and when I relaunch the application then the onCreate method of MyApplication extends with Application class not call.

I see the answer here https://stackoverflow.com/a/53334788/10069542

<service
android:enabled="true"
android:name=".ExampleService"
android:exported="false"
android:stopWithTask="true" />

When I write android:stopWithTask="true", it's working fine in android OS version 9 and below but not working in android OS version 10. It's weird when I write android:stopWithTask="false" it's working fine in android OS version 10 but not working any other version.

package com.example.myapplication;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.IBinder;
import android.util.Log;

public class MyService extends Service {
private final String  VIDEO_SERVICE_CHANNEL = 
"VIDEO_SERVICE_CHANNEL";
public static final String TAG = "v_call";

@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onCreate() {
    super.onCreate();
    Log.i(TAG, "MyService onCreate()");
    startForeground(1, getMyNotification("Riaz"));
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.i(TAG, "MyService onStartCommand()");
    return START_NOT_STICKY;
}

@Override
public void onTaskRemoved(Intent rootIntent) {
    super.onTaskRemoved(rootIntent);
    Log.i(TAG, "MyService onTaskRemoved()");
    this.stopSelf();
}

@Override
public void onDestroy() {
    super.onDestroy();
    Log.i(TAG, "MyService onDestroy()");
    this.stopSelf();
}

@Override
public void onLowMemory() {
    super.onLowMemory();
    Log.i(TAG, "onLowMemory()");
}

private Notification getMyNotification(String userName){
    NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        NotificationChannel mChannel = new NotificationChannel(VIDEO_SERVICE_CHANNEL, getApplicationContext().getString(R.string.room_notification_channel_title), NotificationManager.IMPORTANCE_LOW);
        if (notificationManager != null) {
            notificationManager.createNotificationChannel(mChannel);
        }
    }
    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    CharSequence title = getApplicationContext().getString(R.string.room_notification_title);
    PendingIntent contentIntent = PendingIntent.getActivity(this,
            0,intent , 0);



    return new Notification.Builder(this,VIDEO_SERVICE_CHANNEL)
            .setContentTitle(title)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentIntent(contentIntent).build();
}}

Activity code

public class MainActivity extends AppCompatActivity {
Intent mServiceIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

@Override
protected void onStart() {
    super.onStart();
    Log.i(MyService.TAG, "MainActivity onStart()");
    mServiceIntent = new Intent(MainActivity.this, MyService.class);
    startVideoService();
}

@Override
protected void onDestroy() {
    super.onDestroy();
    Log.i(MyService.TAG, "MainActivity onDestroy()");
    stopService(mServiceIntent);
}

private void startVideoService() {
    Log.i(MyService.TAG, "startVideoService()");

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        startForegroundService(mServiceIntent);
    } else {
        startService(mServiceIntent);
    }

}}

Any ideas please?

Thanks in advance

  • what about `onTaskRemoved` mentioned in that answer? – keshav kowshik Apr 27 '20 at 10:38
  • When I write android:stopWithTask="false" then onTaskRemoved called in all version and it's working fine in android OS version 10 but not any other version, and When I write android:stopWithTask="true" then onTaskRemoved not called in android OS version 9 and below and it's working fine in android OS version 9 and below but not working in android OS version 10 – Riaz Yousaf Apr 27 '20 at 11:01
  • then you check for OS version and make sure they both get executed based on version – keshav kowshik Apr 27 '20 at 11:04
  • are you returning `START STICKY` in your service. can you post your service code? – keshav kowshik Apr 27 '20 at 11:07
  • I have try returning with START STICKY and START_NOT_STICKY but not success – Riaz Yousaf Apr 27 '20 at 11:50

2 Answers2

0

@Override public void onTaskRemoved(Intent rootIntent) {

    Intent intentz = new Intent(this, MapService.class);
    stopService(intentz);

    super.onTaskRemoved(rootIntent);
}
Lucky Les
  • 1
  • 1
  • 2
-1

I had the same problem, solved it this way...

@Override
public void onTaskRemoved(Intent rootIntent) {
   
   Intent intentz = new Intent(this, MapService.class);
   
   stopService(intentz);

   super.onTaskRemoved(rootIntent);
}
Luis Paulo Pinto
  • 5,578
  • 4
  • 21
  • 35
Lucky Les
  • 1
  • 1
  • 2