0

i have a service for connecting my socket to server and i want restart this service when my app kill.

restarting service works fine on android 5 but not working on Oreo.

i tried so many ways to do this like : boardCastReceivers and changing manifest setting for service but not working

my codes: in manifest


       <service
            android:name="com.jeen.jeen.service.ChatService"
            android:enabled="true" >
        </service>

        <receiver
            android:name="com.jeen.jeen.service.ChatReceiver"
            android:enabled="true"
            android:exported="true"
            android:label="RestartServiceWhenStopped"
            >
        </receiver>




in MainActivity:




@Override
    protected void onDestroy() {
        stopService(mServiceIntent);
        Log.i("MAINACT", "onDestroy!");
        super.onDestroy();

    }



    private boolean isMyServiceRunning(Class<?> serviceClass) {
        ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
            if (serviceClass.getName().equals(service.service.getClassName())) {
                Log.i ("isMyServiceRunning?", true+"");
                return true;
            }
        }
        Log.i ("isMyServiceRunning?", false+"");
        return false;
    }


    ChatService chatService;
    Intent mServiceIntent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        chatService = new ChatService(MainActivity.this);
        mServiceIntent = new Intent(MainActivity.this, chatService.getClass());
        if (!isMyServiceRunning(chatService.getClass())) {
            startService(mServiceIntent);
        }



        setContentView(R.layout.activity_main);


        }



receiver class: 


public class ChatReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i(ChatReceiver.class.getSimpleName(), "Service fucked up");
        context.startService(new Intent(context, ChatService.class));
    }
}




and service class:




public class ChatService extends Service{

    public static Socket socket;

    @Override
    public void onCreate() {
        super.onCreate();
        Log.e("onCreate", "onCreate");
        try {
            socket = IO.socket("http://192.168.174.1:8000");
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
//        Toast.makeText(AppController.getInstance(),"Created",Toast.LENGTH_SHORT).show();
    }

    public ChatService(Context applicationContext) {
        super();
        Log.i("HERE", "here I am!");
    }

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

        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
            onTaskRemoved(intent);
        }

        super.onStartCommand(intent, flags, startId);


        Timer t = new Timer();
        t.scheduleAtFixedRate(new TimerTask() {
                                  @Override
                                  public void run() {

                                      new Handler(Looper.getMainLooper()).post(new Runnable() {
                                          @Override
                                          public void run() {

                                              if (!socket.connected()) {
                                                  socket.connect();
                                              }

                                          }
                                      });


                                  }

                              },
                0,
                5000);




        return START_STICKY;
    }
    @Override
    public void onDestroy() {
        Log.i("EXIT", "ondestroy!");

        super.onDestroy();

    }


    @Override
    public void onTaskRemoved(Intent rootIntent) {
        super.onTaskRemoved(rootIntent);


        Intent restartServiceIntent = new Intent(getApplicationContext(), ChatService.class);
        restartServiceIntent.setPackage(getPackageName());

        PendingIntent restartServicePendingIntent = PendingIntent.getService(getApplicationContext(), 1, restartServiceIntent, PendingIntent.FLAG_ONE_SHOT);
        AlarmManager alarmService = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
        alarmService.set(
                AlarmManager.ELAPSED_REALTIME,
                SystemClock.elapsedRealtime() + 4000,
                restartServicePendingIntent);


    }

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











is anyway to do this?

Markus Kauppinen
  • 3,025
  • 4
  • 20
  • 30
Ehsan.F
  • 35
  • 7
  • i correctly started service but can't restarting – Ehsan.F Jun 27 '19 at 10:21
  • 1
    You didn't even read the link, it clearly states you need foreground service with sticky notification. Requests to start background services when your app is not active will be denied. – Pawel Jun 27 '19 at 11:00
  • right, i read that carefully and solved. thank you a lot – Ehsan.F Jun 27 '19 at 21:46

1 Answers1

0

If you are using vivo/honor/oppo etc , your services will get killed as soon as your app is closed.

Akhil Sudha
  • 53
  • 1
  • 9