Android的ActivityManager類是Android系統中的一個重要管理類,負責管理應用程序的Activity。下面是一些常見的ActivityManager使用案例:
Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent);
finish();
ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> runningTasks = activityManager.getRunningTasks(1);
String currentActivity = runningTasks.get(0).topActivity.getClassName();
Log.d(TAG, "Current Activity: " + currentActivity);
ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
activityManager.killBackgroundProcesses("com.example.app");
ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> runningAppProcesses = activityManager.getRunningAppProcesses();
for (ActivityManager.RunningAppProcessInfo processInfo : runningAppProcesses) {
Log.d(TAG, "Process Name: " + processInfo.processName);
}
總結:
ActivityManager是Android系統中重要的管理類,可以通過它來啟動、銷毀、獲取當前運行的Activity和應用程序進程等操作。根據具體的需求,可以靈活運用ActivityManager來管理Android應用程序。