How to Fetch Currently Running Tasks in Android Programmatically? Last Updated : 23 Jan, 2022 Comments Improve Suggest changes Like Article Like Report Android devices run several tasks in the background for providing a seamless experience to the users. Importantly, tracking background and foreground usages is important as it directly impacts the battery life and static memory, i.e. RAM. One can enable the developer option in the settings to track all the background tasks and services. However, one can even programmatically find out the same. So in this article, we will show you how you could programmatically fetch currently running tasks in Android. Follow the below steps once the IDE is ready. Step by Step Implementation Step 1: Create a New Project in Android Studio To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. We demonstrated the application in Kotlin, so make sure you select Kotlin as the primary language while creating a New Project. Step 2: Working with the activity_main.xml file Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. We added a TextView to display the list of running tasks. XML <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <TextView android:id="@+id/text_view" android:layout_width="match_parent" android:layout_height="match_parent" android:scrollbars="vertical"/> </RelativeLayout> Step 3: Working with the MainActivity.kt file Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail. In the main code, primarily, we shall be using the ActivityManager to get the list of running tasks. Kotlin package org.geeksforgeeks.currentlyrunningapps import android.app.ActivityManager import android.content.Context import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.widget.TextView import java.util.* class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // Declaring and initializing the // TextView from the layout file val mTextView = findViewById<TextView>(R.id.text_view) // Getting the instance of ActivityManager val mActivityManager = getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager // Getting the tasks in the form of a list val mRecentTasks: List<ActivityManager.RunningTaskInfo> = Objects.requireNonNull(mActivityManager).getRunningTasks(Int.MAX_VALUE) var mString = "" // Creating a string of each index of the list for (i in mRecentTasks.indices){ mString = mString + " " + mRecentTasks[i].baseActivity?.toShortString() + " " + mRecentTasks[i].id + "\n\n" } // Setting the TextView with the string mTextView.text = mString } } Output: You can see the list of running tasks as shown below. Comment More infoAdvertise with us Next Article How to Fetch Currently Running Tasks in Android Programmatically? aashaypawar Follow Improve Article Tags : Kotlin Android Similar Reads How to Fetch Device ID in Android Programmatically? The android Device ID is a unique code, string combinations of alphabets and numbers, given to every manufactured android device. This code is used to identify and track each android device present in the world. In Android, the Device ID is typically related to the Google Play Services and is most c 3 min read How to Get RAM Memory in Android Programmatically? RAM (Random Access Memory) of a device is a system that is used to store data or information for immediate use by any application that runs on the device. Every electronic device that runs a program as a part of its application has some amount of RAM associated with it. Mobile devices nowadays come 3 min read How to Check the Battery Level in Android Programmatically? Sometimes, it is useful to determine the current battery level. One may choose to reduce the rate of your background updates if the battery charge is below a certain level. But one cannot continuously monitor the battery state. In general, the impact of constantly monitoring the battery level has a 2 min read How to Detect Tablet or Phone in Android Programmatically? A Mobile is a portable electronic device that allows you to make calls, send messages, and access the internet, among other functions. A tablet is a mobile computing device with a touchscreen display and typically a larger screen size than a smartphone. Both devices are designed to be portable and a 3 min read How to Get the Device's IMEI and ESN Programmatically in Android? Many times while building Android Applications we require a unique identifier to identify the specific mobile users. For identifying that user we use a unique address or identity. For generating that unique identity we can use the android device id. In this article, we will take a look at How to get 4 min read How to Quit Android Application Programmatically? When we want to implement an exit AlertDialog in our android application we have to programmatically exit our android application. In this article, we will take a look at How to Quit the Android application programmatically. We will be adding a button and on clicking on that button we will be closin 3 min read How to Get Current Default Language of Android Device Programmatically? Smartphones seem to be predicting and showing us results from what we think in our minds. If you think of traveling, you shall soon expect something related to it in your device's feed. This is the next level of technology, where data helps in developing businesses. The software collects personal da 2 min read How to Get the MAC of an Android Device Programmatically? MAC stands for Media Access Control. The MAC address is also known as the Equipment Id Number. This MAC Address is provided by the Network Interface Card. In this article, we will see step by step from creating a new empty project to How to make an android app to display MAC Address using Java. Note 2 min read How to Find the Screen Resolution of a Device Programmatically in Android? Screen Resolution refers to the number of pixels on display. A higher resolution means more pixels and more pixels provide the ability to display more visual information. This entity is widely used in applications related to the broadcasting of real-time visuals such as live video, gaming, etc for o 3 min read How to Get Current Location in Android? As a developer when you work on locations in Android then you always have some doubts about selecting the best and efficient approach for your requirement. So in this article, we are going to discuss how to get the user's current location in Android. There are two ways to get the current location of 5 min read Like