Heryad
Heryad

Reputation: 23

How To Make Background Process in Android Studio

hi guys thank you for answering my question,i have made an android app in android studio i want to make funtion when i close the app the function start automatically in background is there any way to do it (Sorry For My Bad English)

Upvotes: 0

Views: 8540

Answers (2)

Ram
Ram

Reputation: 23

You can use Application class.

    public class App extends Application {

    private static App instance;
    private static Context context;

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

        App.context = getApplicationContext();

        startService(new Intent(this, YourBackgroundService.class));
    }
}

Then in BackgroundService class should be like this :

public class YourBackgroundService extends Service {

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

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

        return super.onStartCommand(intent, flags, startId);
    }
} <br>

Make sure you can declare this class in AndroidManifest.xml

<service android:name=".YourBackgroundService" />


If you declare like this the application will run always in background.

Upvotes: 1

Kundan
Kundan

Reputation: 590

You can use services. Here is the link for the official documentation: https://developer.android.com/guide/components/services.html

Upvotes: 1

Related Questions