[英]How do I kill background tasks in Oreo programmatically?
I'm building a mobile app, and I'd like to implement a functionality where I can clean ram memory by killing background tasks in Android Oreo (something similar like CCleaner would do). 我正在构建一个移动应用程序,我想实现一项功能,通过杀死Android Oreo中的后台任务来清理RAM内存(类似CCleaner的方法)。
I've tried an algorithm I found on a GitHub. 我已经尝试过在GitHub上找到的算法。 But it seemed to only work with Android 5.0 但这似乎只适用于Android 5.0
protected void populateTextViewWithRunningProcesses() {
// Empty the TextView
mTextView.setText("");
// Initialize a new instance of ActivityManager
ActivityManager am = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
// Get a list of RunningAppProcessInfo
List<AndroidAppProcess> processes = AndroidProcesses.getRunningAppProcesses();
// Display the number of running processes
Toast.makeText(mContext, "Running processes : " +
runningProcesses.size(), Toast.LENGTH_SHORT).show();
// Loop through the running processes
for (ActivityManager.RunningAppProcessInfo processInfo : runningProcesses) {
// Get the process name
mTextView.setText(mTextView.getText() + processInfo.processName + "\n");
}
}
It only shows my app's own tasks but not other running tasks 它仅显示我的应用程序自己的任务,而不显示其他正在运行的任务
Android 5.0+ killed getRunningTasks(int) and getRunningAppProcesses(). Android 5.0+终止了getRunningTasks(int)和getRunningAppProcesses()。 Both of those methods are now deprecated and only return your application process. 这两个方法现在都已弃用,仅返回您的应用程序过程。 You can get a list of running apps using UsageStatsManager. 您可以使用UsageStatsManager获取正在运行的应用程序的列表。 Check this example: https://github.com/googlesamples/android-AppUsageStatistics 检查以下示例: https : //github.com/googlesamples/android-AppUsageStatistics
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.