Top Menu

Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Friday 5 May 2017

How to make an android app to always run in background?

If you want to make an Android app to always run in background, then below is the process of doing it.
You have to start a service in your Application class to run it always. If you do that, your service will be always running. Even though user terminates your app from task manager or force stop your app, it will start running again.
Create a service:
public class YourService extends Service {

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

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // do your jobs here
        return super.onStartCommand(intent, flags, startId);
    }
}
Create an Application class and start your service:
public class App extends Application {

    @Override
    public void onCreate() {
        super.onCreate();

        startService(new Intent(this, YourService.class));
    }
}
Don't forget add this in "application" tag of your AndroidManifest.xml
android:name=".App"

No comments:

Post a Comment