0

I am trying to run the background services. Already it's a STICKY service and I have tried lots of answers already on the StackOverflow. But still, the service is not run in the background in oppo device only having version 5.0. If someone is having the solution regarding this. Please help

Here is the code of my service class

public class MultipleChatBackgroundService extends Service {
private ServiceCallbacks serviceCallbacks;
public static boolean isrunning = false;
private String mychannel;

public interface ServiceCallbacks {
    void update(ArrayList<Message_Bean> message, boolean type);
}

@Nullable
@Override
public IBinder onBind(Intent intent) {
    return new MultipleChatBackgroundService.MyBinder();
}

public class MyBinder extends Binder {
    MultipleChatBackgroundService getService() {
        return MultipleChatBackgroundService.this;
    }

}

@Override
public void onCreate() {
    super.onCreate();
    isrunning = true;
    try {
        if (!Constants.MYCHANNEL.isEmpty() && Constants.MYCHANNEL != null)
            mychannel = Constants.MYCHANNEL;


    } catch (Exception e) {
        e.printStackTrace();
    }

}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    return START_STICKY;
}

public void recieveMessage(ArrayList<Message_Bean> messageBeans, String channel_id,
                           String msg) {
    FcmListenerService fcmListenerService = new FcmListenerService();
    if (serviceCallbacks != null) {

        serviceCallbacks.update(messageBeans, true);
        //chat notification in foreground
        if (!AppController.isActivityVisible()) {
            fcmListenerService.sendNotification(this, messageBeans, channel_id, msg);
        }

    } else {
        if (this != null) {
            //If activity is visible then update the chat
            if (AppController.isActivityVisible()) {
                Intent intent = new Intent("activity.chatactivity");
                intent.putExtra("update_chat", new Gson().toJson(messageBeans));
                sendBroadcast(intent);

            } else {
                if (Constants.allChannelNames.contains(channel_id)) {
                    if (Integer.parseInt(messageBeans.get(0).getSender_id()) != PrefsManager.with(this).getObject(
                            Constants.PREF_USER, PojoDefault.class).getResponse().getUser_id()) {
                        Log.d("notification", "working");
                        fcmListenerService.sendNotification(this, messageBeans, channel_id, msg);
                    }

                }

            }
        }
    }
}

@Override
public void onDestroy() {
    super.onDestroy();
    isrunning = false;
}

public void setCallbacks(MultipleChatBackgroundService.ServiceCallbacks callbacks) {
    serviceCallbacks = callbacks;
}

public void clearCallbacks() {
    serviceCallbacks = null;
}

@Override
public void onTaskRemoved(Intent rootIntent) {
    Intent restartServiceTask = new Intent(getApplicationContext(), this.getClass());
    restartServiceTask.setPackage(getPackageName());
    PendingIntent restartPendingIntent = PendingIntent.getService(getApplicationContext(), 1, restartServiceTask, PendingIntent.FLAG_ONE_SHOT);
    AlarmManager myAlarmService = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
    myAlarmService.set(
            AlarmManager.ELAPSED_REALTIME,
            SystemClock.elapsedRealtime() + 1000,
            restartPendingIntent);

    super.onTaskRemoved(rootIntent);
}

}

I.d007
  • 156
  • 3
  • 16

1 Answers1

0

Set the attribute "stopWithTask"=false in corresponding <service> tag of manifest file.

or use below code :

@Override
public void onTaskRemoved(Intent rootIntent) {
    Intent restartService = new Intent(getApplicationContext(),
            this.getClass());
    restartService.setPackage(getPackageName());
    PendingIntent restartServicePI = PendingIntent.getService(
            getApplicationContext(), 1, restartService,
            PendingIntent.FLAG_ONE_SHOT);
    AlarmManager alarmService = (AlarmManager)getApplicationContext().getSystemService(Context.ALARM_SERVICE);
    alarmService.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() +1000, restartServicePI);
}
Ankit Jain
  • 46
  • 4