0

I am calling service from the application class, If I close the app or remove from currently running apps, then the service will destroy automatically,I didn't written any code in on Destroy() method

to call service here is code :

 Intent syncIntent = new Intent(this, ScanBLE_Service.class);
 this.startService(syncIntent);

here is the code of service class

public class ScanBLE_Service extends IntentService {


  public ScanBLE_Service() {
    super(ScanBLE_Service.class.getName());
    // TODO Auto-generated constructor stub

    mHandler = new Handler();

}

@Override
protected void onHandleIntent(Intent intent) {
    // TODO Auto-generated method stub
                 demo();
}}

    private void demo() {

    mHandler.removeCallbacksAndMessages(null);
    mHandler.postDelayed(new Runnable() {
                             @Override
                             public void run() {
                                  Toast.makeText(getApplicationContext(), "Demoooo", Toast.LENGTH_SHORT).show();

                    demo();
                 }
            }, 5000
    );
}

4 Answers4

1

You should use Service, not IntentService. Extend the Service class and override onStartCommand method, then do the calculations in this method

Shayan_Aryan
  • 2,002
  • 1
  • 29
  • 31
  • Thank you @Shayan_Aryan, I extended the Service instead Intent Service. Now my service is running even if i removed app from running app. – prashant date Jun 29 '16 at 11:03
1

To run your service even after the application is destroyed you need to do the following.

  1. extend your service with Service classs
  2. return START_STICKY in onStartCommand()
  3. override onTaskRemoved(refer the following example code).

    public class MyIntentService extends Service { Timer mTimer;

        @Override
        public void onCreate()
        {
            super.onCreate();
    
            mTimer = new Timer();
    
        }
    
        @Override
        public int onStartCommand(Intent intent, int flags, int startId)
        {
            mTimer.schedule(mTimerTask, 1000, 5000);
    
            return START_STICKY;
        }
    
        @Nullable
        @Override
        public IBinder onBind(Intent intent)
        {
            return null;
        }
    
    
        TimerTask mTimerTask = new TimerTask()
        {
            @Override
            public void run()
            {
              System.out.println("timer task run");
    
            }
        };
    
    
        @Override
        public void onTaskRemoved(Intent rootIntent)
        {
            System.out.println("onTaskRemoved");
    
            Intent restartServiceIntent = new Intent(getApplicationContext(), this.getClass());
            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() + 1000, restartServicePendingIntent);
    
            super.onTaskRemoved(rootIntent);
        }
    
    }
    
Kumar M
  • 994
  • 6
  • 21
0

Yes, the service will be closed if application is closed. One situation, when the service will not be closed in to have a constant Notification on notifications screen. More here

Community
  • 1
  • 1
R. Zagórski
  • 20,020
  • 5
  • 65
  • 90
0

The normal behavior is that the service will keep running even if the application is closed, because it's separated from the application and runs in background, unless you call stopSelf() or stopService(x,x) it should keep running..

PS: there's another type of service which is IntentService (extends IntentService rather then Service) and this will stop automatically once the code in onHandleIntent() is executed..

Shareefoo
  • 41
  • 2