Java Code Examples for android.app.ActivityManager#killBackgroundProcesses()

The following examples show how to use android.app.ActivityManager#killBackgroundProcesses() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AppApplicationMgr.java    From AndroidWallet with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 结束进程
 *
 * @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();
    }
}
 
Example 2
Source File: UIUtils.java    From MinMinGuard with GNU General Public License v3.0 6 votes vote down vote up
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();
    }
}
 
Example 3
Source File: AppManager.java    From qingyang with Apache License 2.0 6 votes vote down vote up
/**
 * 退出应用程序
 * 
 * @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);
}
 
Example 4
Source File: KILL.java    From stynico with MIT License 6 votes vote down vote up
/**
 * 一键清理
 */
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);
}
 
Example 5
Source File: smali_layout_apktool.java    From stynico with MIT License 6 votes vote down vote up
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);
}
 
Example 6
Source File: OBSystemsManager.java    From GLEXP-Team-onebillion with Apache License 2.0 6 votes vote down vote up
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);
        }
    }
}
 
Example 7
Source File: AppUtils.java    From Common with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
    }
}
 
Example 8
Source File: ProcessManagerActivity.java    From MobileGuard with MIT License 5 votes vote down vote up
/**
 * 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();

}
 
Example 9
Source File: CleanUtil.java    From CleanExpert with MIT License 5 votes vote down vote up
public static void killAppProcesses(String packageName) {
    if (packageName == null || packageName.isEmpty()) {
        return;
    }

    ActivityManager am = (ActivityManager)MyApplication.getInstance()
            .getSystemService(Context.ACTIVITY_SERVICE);
    am.killBackgroundProcesses(packageName);
}
 
Example 10
Source File: AppManager.java    From MvpRoute with Apache License 2.0 5 votes vote down vote up
/**
 * 退出应用程序
 */
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) {
	}
}
 
Example 11
Source File: AppManager.java    From YiZhi with Apache License 2.0 5 votes vote down vote up
/**
 * 退出应用程序
 */
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) {
    }
}
 
Example 12
Source File: AppManager.java    From AndroidStudyDemo with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 退出应用程序
 */
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);
    }
}
 
Example 13
Source File: HomeFragment.java    From FireFiles with Apache License 2.0 5 votes vote down vote up
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);
    }
}
 
Example 14
Source File: HomeFragment.java    From FireFiles with Apache License 2.0 5 votes vote down vote up
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);
    }
}
 
Example 15
Source File: AppManager.java    From AcgClub with MIT License 5 votes vote down vote up
/**
 * 退出应用程序
 */
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();
  }
}
 
Example 16
Source File: AppManager.java    From ZZShow with Apache License 2.0 5 votes vote down vote up
/**
 *  退出应用程序
 */
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();
    }
}
 
Example 17
Source File: AppManager.java    From Pigeon with MIT License 5 votes vote down vote up
/**
 * 退出应用程序
 */
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();
    }
}
 
Example 18
Source File: Utils.java    From always-on-amoled with GNU General Public License v3.0 5 votes vote down vote up
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);
    }
}
 
Example 19
Source File: MainService.java    From More-For-GO with GNU General Public License v3.0 5 votes vote down vote up
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);
    }
}
 
Example 20
Source File: U.java    From SecondScreen with Apache License 2.0 5 votes vote down vote up
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;
}