Programmatically uninstall an APK

DKCERT

Member
Licensed User
Longtime User
Hi all,

Very nice development suite!

Question:

Is it possible to uninstall an App programmatically using B4A? Like in the scenario that it has been found rogue or violate a mobile security policy.

Thanks in advance.
 

DKCERT

Member
Licensed User
Longtime User
You can uninstall an application, but not without the user approving the uninstall. Android doesn't allow silent installs / uninstalls.

Thank you for the quick reply.

Is there by any chance demo-code (could not find any when searching the forum) on how to invoke an uninstall of an app? I don't see anything for doing so in the PackageManager documentation (Basic4android - Phone)
 
Upvote 0

Kim Rowden

Member
Licensed User
Longtime User
Thanks Erel - if I could add to this discussion to show how one can collect the information about such an intent...

If you don't have a usable B4A app yet you can get the system log contents using a free app from the Android Market:

1. First downloaded a log capture app from the Android Market. Here are a couple of suggestions:
Log Collector by Xtralogic
CatLog by Nolan Lawson

2. Then install any simple app - I used Bubble by Ben Zibble (a very handy app by the way)
The only reason to install this app is to uninstall it and take a look at the resulting log.

3. Once your choice of logging app and the Bubble app are installed you can go to Settings | Applications | Manage Applications
Then find the Bubble app and uninstall it.

4. Now go back to the home page and run your logging app. I used Log Collector because it allows me to easily send the log to myself via email.

5. Once you have the log file you can search through it for the text "android.intent.action.DELETE"
This line should show you the Intent the system used to remove the Bubble app. Here is what my log displayed:

08-29 13:47:30.458 I/ActivityManager( 1261): Starting activity: Intent { act=android.intent.action.DELETE dat=package:bz.ktk.bubble cmp=com.android.packageinstaller/.UninstallerActivity }

If you do have a usable (runnable) B4A app you can view the log contents in the B4A Log panel (on the righthand side) - even though your app will be idle you can still remove the Bubble app and see the log contents as before. Just make sure the Filter checkbox is NOT checked.



To use Erel's code to remove the Bubble app in your own B4A app you would then need to use the results from above in your code:

B4X:
Dim i As Intent
i.Initialize("android.intent.action.DELETE", "package:bz.ktk.bubble")
StartActivity(i)

In addition to performing system tasks such as the uninstall you can also use this approach to determine how to run any other intent that a 3rd party app might support - such as downloading a PDF file from a website to the SDcard ... and then using it again to open that local PDF file.

It's all pretty straight forward once you see how it is done in the log file.
I hope this helps,
-Kim
 
Upvote 0

DKCERT

Member
Licensed User
Longtime User
Wow... Thank you both for the quick info. Very, very helpful indeed!

I have made these subs for the purpose:

B4X:
Sub GetTheInstalledApps
   ' http://www.b4x.com/android/help/phone.html#packagemanager
   ' http://developer.android.com/reference/android/content/pm/ApplicationInfo.html
   Dim i As Int
   Dim sStr As String
   Dim pm As PackageManager
   Dim Packages As List
   If ListView1.Size > 0 Then ListView1.Clear
   ListView1.SingleLineLayout.Label.TextSize = 14
   Packages = pm.GetInstalledPackages
   For i = 0 To Packages.Size - 1
      ' AppFlags = pm.GetApplicationFlags(Packages.Get(i)) ' ???
      ' sStr = pm.GetApplicationLabel(Packages.Get(i)) & " v/" & pm.GetVersionName(Packages.Get(i)) & " : " & Packages.Get(i)
      ' ListView1.AddSingleLine(sStr)
      ListView1.AddSingleLine(Packages.Get(i))
   Next
   ListView1.SetSelection(0)
End Sub

Sub UninstallApp(sRealAppName As String)
   Dim i As Intent
   i.Initialize("android.intent.action.DELETE", "package:" & sRealAppName)
   StartActivity(i)
End Sub

As I'm only interrested in the User installed Apps I found another thread where the flag is used to determine if it's a System App.

Is there any native B4A way to get the flags (the Reflection lib is a bit too complicated at present time). Something like AppFlags = pm.GetApplicationFlags(Packages.Get(i)) or similar?
 
Upvote 0

Jamsa

Member
Licensed User
Longtime User
I've used this code and it works fine...when I only need to uninstall ONE app, but if I want to uninstall more apps, I can't get it to do more than one.

I've looked in the log, and was puzzled by the fact that my code actually works, but it runs "too fast". Meaning it tries to start uninstalling the next app, before the first one has even been approved by the user.

I've tried adding a check variable to see when the uninstalling has finished, but as my code runs while the user is active, it doesn't really help. :-/

my uninstall subs are in a codemodule - is that my problem, or something else...?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

Jamsa

Member
Licensed User
Longtime User
Wow, I actually need to use a server...! :O

Hmm...can I include it in my current project, or do I have to have a seperate app just for this?
 
Upvote 0

Jamsa

Member
Licensed User
Longtime User
Now I got it working. Yay! :)

But then I found an issue...if a user selects some apps for uninstall and start the process, but then decides NOT to uninstall one of the apps, the intent keeps asking for uninstalling until the user press OK! :D

lol, how do I catch the cancel of an uninstall?
 
Upvote 0

Jamsa

Member
Licensed User
Longtime User
Actually that wasn't the problem :p

But it made me find the solution, so now it works completely.

Thanks for your help. :):)
 
Upvote 0
Top