Search code examples
androidservicebackground-process

Background service after closing activity in Android


I try to create application, that check some remote database for news and activities. When app is running, it's work ok, app checks, shows notification, etc. But if user close app, everything stops. Is it possible to create background service in the application, that works after application closed (for send local notification, for example)? And if it's possible, what I have to do?

P.S. I know about Firebase and Google Cloud, but I want to create independent app.


Solution

  • As @Flo We said Service is the only option you can use
    
    public class BackGroundService extends Service{
        @Nullable
        @Override
        public IBinder onBind(Intent intent) {
            return null;
        }
    
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            Log.e("Service","Started");
    
    //Write your code here to run in background
    
    
            return super.onStartCommand(intent, flags, startId);
        }
    
    }
    

    start service using Intent, don't stop the service on onDestroy and it will continue running in background