0

I have an app with two processes. The second process starts when an Activity is created. Find below an excerpt of that Activity in the Manifest:

    <activity
        android:name=".ActivityInAnotherProcess"
        android:process=":anotherprocess"
        android:launchMode="singleTask"
        ...

After the ":anotherprocess" starts I need to kill the main process somehow, through the adb, in code, however.

I've tried "Terminate Application" in the DDMS and the main process is killed but recreated after a few seconds.

I've tried this code:

String packageName = c.getPackageName();
ActivityManager activityManager = (ActivityManager) c.getSystemService(Context.ACTIVITY_SERVICE);
activityManager.killBackgroundProcesses(packageName);

but the main process is killed only to be recreated after a few seconds.

UPDATE: The code posted above works. I was calling it a few seconds after starting the Activity in the other process but it seems the other process was not fully started before I killed the main process. Now I'm killing the main process from the other process. This works now. Thanks all!

dalmendray
  • 51
  • 1
  • 6
  • have you tried? android.os.Process.killProcess(android.os.Process.myPid()); – Alan Godoi Jan 19 '17 at 02:56
  • @dalmendray Why you want to kill the background process? – Charuක Jan 19 '17 at 02:57
  • @Charuka I need to kill the main process to simulate when Android does it. I have interprocess communication in my code that I need to test based on the fact that the main process is killed (as it could be if Android needs more resources) – dalmendray Jan 25 '17 at 23:07

1 Answers1

1
List<ApplicationInfo> packages;
PackageManager pm;
pm = getPackageManager();
packages = pm.getInstalledApplications(0);

ActivityManager mActivityManager = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);


for (ApplicationInfo packageInfo : packages) {
    if((packageInfo.flags & ApplicationInfo.FLAG_SYSTEM)==1)continue;
    if(packageInfo.packageName.equals("mypackage")) continue;
    mActivityManager.restartPackage(packageInfo.packageName);
} 

if API >= 8 use mActivityManager.killBackgroundProcesses(String packageName)

if API < 8 use mActivityManager.restartPackage(packageInfo.packageName);

Ramkumar.M
  • 681
  • 6
  • 21
  • 1
    Why doing the for loop if you already know the package name? Should be better to just call mActivityManager.killBackgroundProcesses("mypackage") – dalmendray Jan 25 '17 at 23:32