I want to get a list of active or re-running apps in android to be able for user to terminate from the list shown on the screen but maybe something has gone wrong because when I touch refresh button no new apps are shown
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = findViewById(R.id.list_view);
activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
runningAppsList = new ArrayList<>();
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_multiple_choice, runningAppsList);
listView.setAdapter(adapter);
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
// Refresh the app list every hour
listView.postDelayed(new Runnable() {
@Override
public void run() {
updateRunningAppsList();
listView.postDelayed(this, 3600000);
}
}, 3600000);
// Select all apps when the "Select All" button is clicked
Button selectAllButton = findViewById(R.id.select_all_button);
selectAllButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int count = adapter.getCount();
for (int i = 0; i < count; i++) {
listView.setItemChecked(i, true);
}
Toast.makeText(MainActivity.this, "All apps selected", Toast.LENGTH_SHORT).show();
}
});
// Kill the selected apps when the "Kill" button is clicked
Button killButton = findViewById(R.id.kill_button);
killButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int count = adapter.getCount();
for (int i = 0; i < count; i++) {
if (listView.isItemChecked(i)) {
activityManager.killBackgroundProcesses(adapter.getItem(i).packageName);
}
}
// Wait for 1 second to let the terminated apps get removed from the running apps list
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
updateRunningAppsList();
}
}, 1000);
}
});
// Start the service that monitors terminated apps
appTerminatedService = new AppTerminatedService();
Intent intent = new Intent(this, AppTerminatedService.class);
startService(intent);
}
private void updateRunningAppsList() {
runningAppsList.clear();
PackageManager packageManager = getPackageManager();
List<ApplicationInfo> allAppsList = packageManager.getInstalledApplications(PackageManager.GET_META_DATA);
for (ApplicationInfo appInfo : allAppsList) {
if ((appInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
runningAppsList.add(appInfo);
}
}
adapter.notifyDataSetChanged();
}
public class AppTerminatedService extends Service {
private ActivityManager.RunningAppProcessInfo mProcessInfo;
private ArrayList<String> mRunningApps;
private PackageManager mPackageManager;
private Handler mHandler = new Handler();
private boolean mIsTerminated = false;
private Runnable mUpdateApps = new Runnable() {
public void run() {
updateRunningApps();
mHandler.postDelayed(this, 1000);
}
};
public AppTerminatedService() {
mRunningApps = new ArrayList<>();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
mHandler.postDelayed(mUpdateApps, 1000);
return START_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
private void updateRunningApps() {
ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
mPackageManager = getPackageManager();
// Get the list of running processes
List<ActivityManager.RunningAppProcessInfo> runningProcessList = activityManager.getRunningAppProcesses();
// Add the names of all running apps to a list
ArrayList<String> runningApps = new ArrayList<>();
for (ActivityManager.RunningAppProcessInfo processInfo : runningProcessList) {
String appName = "";
try {
appName = mPackageManager.getApplicationLabel(mPackageManager.getApplicationInfo(processInfo.processName, PackageManager.GET_META_DATA)).toString();
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
runningApps.add(appName);
}
// Check if any apps have been terminated
if (mIsTerminated) {
ArrayList<String> terminatedApps = new ArrayList<>(mRunningApps);
terminatedApps.removeAll(runningApps);
// Remove the terminated apps from the running apps list
for (String app : terminatedApps) {
mRunningApps.remove(app);
}
mIsTerminated = false;
}
// Update the running apps list
for (String app : runningApps) {
if (!mRunningApps.contains(app)) {
mRunningApps.add(app);
}
}
}
@Override
public void onTaskRemoved(Intent rootIntent) {
super.onTaskRemoved(rootIntent);
mIsTerminated = true;
}
}
}
I only got my app name in the list and laucher but no more other what is right method to get list of all installed and apps which can be in rerun state