Android Question How can I use these .java files so my B4A app can send ADB commands to the same device it is on?

JohnC

Expert
Licensed User
Longtime User
I would like my B4A app to be able to kill another app running on the same device.

I came across this java code that would allow my app to interface with the ADB shell on the same device as my app (as if a different computer was sending an ADB command to the device):


So it seems my app would be able to send the device an ADB command, such as "adb shell am force-stop <PACKAGE>" to kill another app running on the same device.

Being able to kill the app this way seems to be a better way than these other methods because these other methods may not work on newer versions of android:

B4X:
android.os.Process.killProcess(pid)
activityManager.forceStopPackage(packageName)

So, what is involved to use these java files in a B4A project?
 
Last edited:

drgottjr

Expert
Licensed User
Longtime User
this will open the process door for you. and may god have mercy on your soul.

B4X:
Dim jo As JavaObject
jo.InitializeStatic("android.os.Process")
If Not(jo.IsInitialized) Then
    Log("not initialized")
Else
    Log(jo.RunMethod("myPid",Null))
End If

presumably, once you have the pid and the packagename, you can run your methods
 

Attachments

  • process.png
    process.png
    17.3 KB · Views: 50
Upvote 0

JohnC

Expert
Licensed User
Longtime User
But if I use that native method to kill the process, won't that require my app to have the "android.permission.KILL_BACKGROUND_PROCESSES" permission in the manifest?

If so, then that's what I am concerned about because newer versions of android might not allow that permission.

But, if I am able to use this alternate ADB command method, I think it will work without that permission.
 
Last edited:
Upvote 0

Sandman

Expert
Licensed User
Longtime User
I know nothing about this topic really, but I just wanted to add this thought: If it's possible to wield that much power by simply talk directly to ADB, we could assume with high certainty that there would be a lot more malware and other badly behaving apps out there. Which leads me to believe this probably won't work...

Also:
this will open the process door for you. and may god have mercy on your soul.
:D
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
There is an app that uses this method and it's even available in the play store:

 
Upvote 0
Top