Close another app from your app?
It is not possible to close the another app from your app.
It is ethically not correct and there is no official Android API as well.
How to close application from another application in android
This is how i solve this issue. For sending intent:
Intent intent = new Intent("com.hp.walk.CLOSE_APPLICATION");
getApplication().sendBroadcast(intent);
in another application this is how i register receiver to get intent:
@Override
public void onCreate(Bundle savedInstanceState) {
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("com.hp.walk.CLOSE_APPLICATION");
registerReceiver(receiver, intentFilter);
private BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if ("com.hp.walk.CLOSE_APPLICATION".equals(intent.getAction())) {
Log.i(TAG, "received.. ");
finish();
}
}
};
Close(kill) another app in my application by package name
If you want to kill a background app , check the following code
public void amKillProcess(string package_name)
{
ActivityManager am = (ActivityManager)this.GetSystemService(Context.ActivityService);
var runningProcesses = am.RunningAppProcesses;
foreach (RunningAppProcessInfo runningProcess in runningProcesses)
{
if (runningProcess.ProcessName.Contains(package_name))
{
Android.OS.Process.KillProcess(runningProcess.Uid);
}
}
}
And if you want to kill a foreground app, you could use Adb
public class SuUtil
{
private static Java.Lang.Process process;
public static void kill(string packageName)
{
initProcess();
killProcess(packageName);
close();
}
private static void initProcess()
{
if (process == null)
try
{
process = Runtime.GetRuntime().Exec("su");
}
catch (IOException e)
{
}
}
private static void killProcess(string packageName)
{
System.IO.Stream output = process.OutputStream;
Java.Lang.String cmd = new Java.Lang.String("am force-stop " + packageName + " \n");
try
{
output.Write(cmd.GetBytes());
output.Flush();
}
catch (IOException e)
{
}
}
private static void close()
{
if (process != null)
try
{
process.OutputStream.Close();
process = null;
}
catch (IOException e)
{
}
}
}
Related Topics
Converting JSON String to Java Object
Hibernate: Automatically Creating/Updating the Db Tables Based on Entity Classes
Converting Utf-8 to Iso-8859-1 in Java - How to Keep It as Single Byte
Reading a .Txt File Using Scanner Class in Java
Java.Net.Unknownhostexception: Invalid Hostname for Server: Local
How to Get the First Non-Null Value in Java
Java Hotspot(Tm) 64-Bit Server Vm Warning: Ignoring Option Maxpermsize
How to Get Country Phone Prefix from Iso
Sending Http Post Request with Android
Android Resource Not Found Exception
Android Proguard JavaScript Interface Fail
How to Convert Rgb Color to Int in Java
How to Resume Android Activity Programmatically from Background
Setting Property 'Source' to 'Org.Eclipse.Jst.Jee.Server:Jsftut' Did Not Find a Matching Property
JPA Support for Java 8 New Date and Time API
Why Comparing Integer with Int Can Throw Nullpointerexception in Java