下面列出了android.app.ActivityManager#killBackgroundProcesses ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 结束进程
*
* @param context
* @param pid
* @param processName
*/
@SuppressLint("MissingPermission")
public static void killProcesses(Context context, int pid, String processName) {
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
String packageName;
try {
if (!processName.contains(":")) {
packageName = processName;
} else {
packageName = processName.split(":")[0];
}
activityManager.killBackgroundProcesses(packageName);
Method forceStopPackage = activityManager.getClass().getDeclaredMethod("forceStopPackage", String.class);
forceStopPackage.setAccessible(true);
forceStopPackage.invoke(activityManager, packageName);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Exit application
*/
public static void exit(Context context, int type) {
switch (type) {
case 0:
// Method 1
System.exit(0);
break;
case 1:
// Method 1
int pid = android.os.Process.myPid();
android.os.Process.killProcess(pid);
break;
case 2:
// Method 1
ActivityManager manager = (ActivityManager) context.getApplicationContext()
.getSystemService(Context.ACTIVITY_SERVICE);
manager.killBackgroundProcesses(context.getApplicationContext().getPackageName());
break;
}
}
public void killBackgroundProcesses()
{
MainActivity.log("OBSystemsManager.killBackgroundProcesses");
ActivityManager activityManager = (ActivityManager) MainActivity.mainActivity.getSystemService(MainActivity.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> procInfo = activityManager.getRunningAppProcesses();
if (procInfo != null)
{
for (ActivityManager.RunningAppProcessInfo process : procInfo)
{
int importance = process.importance;
int pid = process.pid;
String name = process.processName;
//
if (name.equals("manager.main")) continue;
if (importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_SERVICE)
{
continue;
}
MainActivity.log("OBSystemsManager.killBackgroundProcesses: " + name);
activityManager.killBackgroundProcesses(name);
}
}
}
private void b()
{
mHandler.sendEmptyMessage(0x1);
ActivityManager activityManager = (ActivityManager)smali_layout_apktool.this.getSystemService(ACTIVITY_SERVICE);
if (processNamelist != null && processNamelist.size() > 0)
{
for (String processName : processNamelist)
{
activityManager.killBackgroundProcesses(processName);
}
}
mHandler.sendEmptyMessageDelayed(0x2, 2000);
}
/**
* 一键清理
*/
private void b()
{
mHandler.sendEmptyMessage(0x1);
ActivityManager activityManager = (ActivityManager)KILL.this.getSystemService(ACTIVITY_SERVICE);
if (processNamelist != null && processNamelist.size() > 0)
{
for (String processName : processNamelist)
{
activityManager.killBackgroundProcesses(processName);
}
}
mHandler.sendEmptyMessageDelayed(0x2, 2000);
}
/**
* 退出应用程序
*
* @param context
*/
public void AppExit(Context context) {
finishAllActivity();
ActivityManager activityManager = (ActivityManager) context
.getSystemService(Context.ACTIVITY_SERVICE);
// // 2.2之前的rom就用restartPackage之后的就用killBackgroundProcesses
if (BaseApplication.isMethodsCompat(Build.VERSION_CODES.FROYO)) {
activityManager.killBackgroundProcesses(context.getPackageName());
} else {
activityManager.restartPackage(context.getPackageName());
}
System.exit(0);
}
static public void restartApp(Context context, String packageName)
{
ActivityManager am = (ActivityManager) context.getSystemService(Activity.ACTIVITY_SERVICE);
am.killBackgroundProcesses(packageName);
Intent it = context.getPackageManager().getLaunchIntentForPackage(packageName);
Activity a = (Activity) context;
if (it != null)
{
if (a.getCurrentFocus() != null)
{
((InputMethodManager) a.getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(a.getCurrentFocus().getWindowToken(), 0);
}
it.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(it);
}
else
{
Toast.makeText(context, context.getString(R.string.msg_app_launch_fail), Toast.LENGTH_SHORT).show();
}
}
public static String uiRefreshCommand2(Context context, boolean shouldClearHome) {
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
// For better reliability, we execute the UI refresh while on the home screen
Intent homeIntent = new Intent(Intent.ACTION_MAIN);
homeIntent.addCategory(Intent.CATEGORY_HOME);
homeIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if(!shouldClearHome) {
try {
context.startActivity(homeIntent);
} catch (ActivityNotFoundException e) { /* Gracefully fail */ }
}
// Kill all background processes, in order to fully refresh UI
PackageManager pm = context.getPackageManager();
List<ApplicationInfo> packages = pm.getInstalledApplications(0);
for(ApplicationInfo packageInfo : packages) {
if(!packageInfo.packageName.equalsIgnoreCase(context.getPackageName()))
am.killBackgroundProcesses(packageInfo.packageName);
}
// Get launcher package name
final ResolveInfo mInfo = pm.resolveActivity(homeIntent, 0);
final String launcherPackageName = mInfo.activityInfo.applicationInfo.packageName;
if(launcherPackageName.equals(getTaskbarPackageName(context))
|| launcherPackageName.equals("android"))
return "sleep 1";
else
return "sleep 1 && am force-stop " + launcherPackageName;
}
/**
* 退出应用程序
*/
public void AppExit() {
try {
killAll();
if (mActivityList != null) {
mActivityList = null;
}
ActivityManager activityMgr =
(ActivityManager) mApplication.getSystemService(Context.ACTIVITY_SERVICE);
activityMgr.killBackgroundProcesses(mApplication.getPackageName());
System.exit(0);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 退出应用程序
*/
public void exitApp(Context context) {
try {
finishAllActivity();
ActivityManager activityMgr =
(ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
activityMgr.killBackgroundProcesses(context.getPackageName());
System.exit(0);
} catch (Exception e) {
}
}
public static void killAppProcesses(String packageName) {
if (packageName == null || packageName.isEmpty()) {
return;
}
ActivityManager am = (ActivityManager)MyApplication.getInstance()
.getSystemService(Context.ACTIVITY_SERVICE);
am.killBackgroundProcesses(packageName);
}
/**
* kill all checked processes
*/
private void killSelectedProcesses() {
long totalReleaseMemory = 0;
int releaseCount = 0;
// get activity manager
ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
// kill checked process
// delete from last position
for (int i = checkeds.size() - 1; i > 0; i--) {
if (!checkeds.get(i))
continue;
// get bean
ProcessInfoBean bean = adapter.getItem(i);
if (null == bean)
continue;
System.out.println("position:" + i + " packageName:" + bean.getPackageName());
// kill process
am.killBackgroundProcesses(bean.getPackageName());
// add count
releaseCount++;
totalReleaseMemory += bean.getMemory();
// remove app from list. Because the list is CopyOnWriteArrayList. So it can remove directly.
if (bean.isSystemApp()) {
systemApps.remove(bean);
} else {
userApps.remove(bean);
}
checkeds.remove(i);
}
// notify refresh ListView
adapter.notifyDataSetChanged();
// show tips
String tips = getString(R.string.tips_kill_process, releaseCount, Formatter.formatFileSize(this, totalReleaseMemory));
Toast.makeText(this, tips, Toast.LENGTH_SHORT).show();
}
/**
* 退出应用程序
*/
public void AppExit(Context context) {
try {
finishAllActivity();
ActivityManager activityMgr =
(ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
activityMgr.killBackgroundProcesses(context.getPackageName());
System.exit(0);
} catch (Exception e) {
}
}
/**
* 退出应用程序
*/
public void AppExit(Context context) {
try {
removeAllActivity();
ActivityManager activityMgr = (ActivityManager) context
.getSystemService(Context.ACTIVITY_SERVICE);
activityMgr.killBackgroundProcesses(context.getPackageName());
System.exit(0);
} catch (Exception e) {
Logger.e(e);
}
}
public void cleanupMemory(Context context){
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> runningProcessesList = getRunningAppProcessInfo(context);
for (ActivityManager.RunningAppProcessInfo processInfo : runningProcessesList) {
activityManager.killBackgroundProcesses(processInfo.processName);
}
}
public void cleanupMemory(Context context){
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> runningProcessesList = getRunningAppProcessInfo(context);
for (ActivityManager.RunningAppProcessInfo processInfo : runningProcessesList) {
activityManager.killBackgroundProcesses(processInfo.processName);
}
}
/**
* 退出应用程序
*/
public void AppExit(){
try{
killAll();
if(mActivityList != null){
mActivityList = null;
}
ActivityManager activityManager = (ActivityManager) mApplication.getSystemService(Context.ACTIVITY_SERVICE);
activityManager.killBackgroundProcesses(mApplication.getPackageName());
System.exit(0);
}catch (Exception e){
e.printStackTrace();
}
}
/**
* 退出应用程序
*/
public void AppExit(Context context) {
try {
finishAllActivity();
ActivityManager activityMgr = (ActivityManager) context
.getSystemService(Context.ACTIVITY_SERVICE);
activityMgr.killBackgroundProcesses(context.getPackageName());
System.exit(0);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void killBackgroundProcesses(Context context) {
List<ApplicationInfo> packages;
PackageManager pm;
pm = context.getPackageManager();
packages = pm.getInstalledApplications(0);
ActivityManager mActivityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
String myPackage = context.getPackageName();
for (ApplicationInfo packageInfo : packages) {
if ((packageInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 1) continue;
if (packageInfo.packageName.equals(myPackage)) continue;
mActivityManager.killBackgroundProcesses(packageInfo.packageName);
}
}
private void killBackgroundProcesses() {
Log.d(MainService.class.getSimpleName(), "Killing background processes");
List<ApplicationInfo> packages = getPackageManager().getInstalledApplications(0);
ActivityManager mActivityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for (ApplicationInfo packageInfo : packages) {
if ((packageInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 1) continue;
if (packageInfo.packageName.contains("com.tomer")) continue;
mActivityManager.killBackgroundProcesses(packageInfo.packageName);
}
}