Jump to content
ertank

Android - Find other app pid number and kill it

Recommended Posts

Hello,

 

I am using Delphi 10.3.2.

 

There is a project where I am communicating with another app (I will call it "other app"). Communication with other app is over clipboard. Other app is developed on Android Studio and is using some embedded device specific SDK. Other app is not a service app. It is a regular app without user interface (completely runs in the background). 

 

If that other app is not running, my app runs it. Waits until it settles completely. Starts communication. I am now asked to close that other app when my app closes. Even if I could run it using an intent, I did not find any Delphi FMX example to close it.

 

Embedded device is running Android 5.1.1. That is unlikely to change in a near future. I believe there maybe no problem of "dangerous permissions" or similar at that Android version.

 

Any help is appreciated.

 

Thanks & regards,

Ertan

Share this post


Link to post

As long as it's just the background processes of the application you're interested in, you could use:

 

https://developer.android.com/reference/android/app/ActivityManager#killBackgroundProcesses(java.lang.String)

 

Since Android 2.2, you cannot close the entire application.

 

You would probably do something like this (untested):

 

uses
  Androidapi.JNI.JavaTypes, Androidapi.JNI.GraphicsContentViewText, Androidapi.JNI.App, Androidapi.Helpers;

procedure Kill;
var
  LService: JObject;
begin
  LService := TAndroidHelper.Context.getSystemService(TJContext.JavaClass.ACTIVITY_SERVICE);
  if LService <> nil then
    TJActivityManager.Wrap(JObjectToID(LService)).killBackgroundProcesses(StringToJString('com.whateverpackagename.itis'));
end;

 

Share this post


Link to post

That works nicely after adding "Kill backgound processes" permission.

 

Thanks.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×