Is it possible to programmatically uninstall a package in Android - android

Can a package uninstall itself? Can a package uninstall another package if they share the same userId and signature?

Uri packageURI = Uri.parse("package:"+"your.packagename.here");
Intent uninstallIntent = new Intent(Intent.ACTION_DELETE, packageURI);
startActivity(uninstallIntent);

A 3rd party app cannot install or uninstall any other packages programmatically, that would be a security risk for Android. However a 3rd party app can ask the Android OS to install or uninstall a package using intents, this question should provide more complete information:
install / uninstall APKs programmatically (PackageManager vs Intents)

In Kotlin, using API 14+, you can just call the following:
startActivity(Intent(Intent.ACTION_UNINSTALL_PACKAGE).apply {
data = Uri.parse("package:$packageName")
})
Or with Android KTX:
startActivity(Intent(Intent.ACTION_UNINSTALL_PACKAGE).apply {
data = "package:$packageName".toUri()
})
It will show the uninstall prompt for your app. You can change packageName to any package name of another app if needed.

Third Party app cannot Uninstall App Silently!
Either you need to become System App to get DELETE_PACKAGES Permission else you need to show Uninstall Popup (User Confirmation)
Alternatively, you can take Accessibility permission and then by showing an Accessibility Overlay you can tell your service to click on Uninstall button! But that will be privacy violation.

Related

How to prompt a message is app already exist in Android

I want to display an alert message on installing a non-market App. For example, If I have an App "ABC" installed in my phone and I want to install its latest version, So on installing, I want to prompt a message "ABC App of is already installed, Do you want to install its latest version ?". If user clicks on yes button, latest App should start installing else installation is terminated.
How do I achieve this?
In your app, use this
PackageInfo pkg = getPackageManager().getPackageInfo(getPackageName(), 0);
String version = pkg.versionName;
int Vcode = pkg.versionCode;
You get the version code of app. Now use a if condition to compare your app version with old version and set alert dialog. Else no alert dialog.
Actually, Now, on android phones if latest version of an application is about to install, android itself prompt a dialog box. on that a list of new permissions needed and an ok also a cancel button . So, you dont have to do it by yourself. :)

how can I perform any task after uninstalling my apk?

I want to install the apk from internal/external memory after uninstalling the same apk.?(actually i want to update my apk,if i directly(don't uninstall the current apk) install my updated apk then there comes signature issue.)
final Intent installIntent = new Intent(Intent.ACTION_VIEW);
if (isSDPresent) {
installIntent.setDataAndType(Uri.fromFile(new File("/storage/sdcard1/Download/" + "Life.apk")), "application/vnd.android.package-archive");
}else {
installIntent.setDataAndType(Uri.fromFile(new File("/storage/emulated/0/Download/" + "Life.apk")), "application/vnd.android.package-archive");
}
installIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(installIntent);
The behaviour you are seeing is by design.
Android does not allow any code within an APK to be run once the user chooses to remove it - there are no notifications and no pre-uninstall actions that you can use. It's designed that way to prevent developers from writing Apps that can never be uninstalled by the user.
Having two apps with the same package name but signed with different keys is also not allowed, and can only be resolved by the user manually removing the first app before installing the second. This is to prevent developers from writing Apps to imitate other Apps (like Facebook for example) without the user knowing about it.
You can update your apk using below command that will upgrade application that is exactly same as when you perform Update from google play store.
adb install -r foo.apk

Is there an intent for uninstallation of an app for ALL users?

Background
The normal way to call for the uninstallation an app is simply by using the "ACTION_DELETE" intent :
startActivity(new Intent(Intent.ACTION_DELETE, Uri.parse("package:" +packageName)));
The problem
starting with some Android version (don't remember which) , apps can be installed for multiple users on the same device.
This means there is a new way to uninstall an app, one which will uninstall it for all users (image taken from Lollipop - Android 5.0 ) :
The question
I've searched in the documentation, but couldn't find the answer those questions:
Is there any way to perform this operation via an intent? Maybe something to add to the intent I've written above ?
Does ADB have a new command to remove an app for all users?
Is there a way to check if an app is installed for multiple users?
Is there any way to perform this operation via an intent? Maybe
something to add to the intent I've written above ?
Yes, but be careful. You can pass in Intent.EXTRA_UNINSTALL_ALL_USERS.
However, it's hidden because it:
should not be part of normal application flow
You could just pass in the constant anyway, if you feel it's necessary and disagree with Google on that one. Just for example, here are the differences between passing in false and true with that constant
final Uri packageURI = Uri.parse("package:" + "some.package.name");
final Intent uninstallIntent = new Intent(Intent.ACTION_UNINSTALL_PACKAGE, packageURI);
uninstallIntent.putExtra("android.intent.extra.UNINSTALL_ALL_USERS", false or true);
startActivity(uninstallIntent);
Results
Does ADB have a new command to remove an app for all users?
No, the command remains the same.
`adb uninstall 'some.package.name'`
This will remove that app for all users. I'm unaware of a way to specify a particular user.
Is there a way to check if an app is installed for multiple users?
No, not that I'm aware of. In fact, when the Settings apps decides to place the "Uninstall for all users" option in the options menu, it's basically doing so based on whether or not there are multiple users period, not if both the current user and another user have an app installed.
Not to mention, most of the methods in UserManager that you'd need to even tell if there are multiple users on the device, like UserManager.getUserCount, require the MANAGE_USERS permission which is a system API and hidden. So, I'm not even sure why that's a public method.
Also, you can easily test all of your questions, much like I did, by creating a dummy user on your device. You don't even need to log into a Google account.

How to check some dependency before installation of an application in Android

I make two android application first one is installer app that download and install my application.
Now i just want to make second application that would not be install from outside installer, means before installing second application it will check that first app is installed or not, if first application is not install it will recommend that it is necessary to install first application before second application.
Please give me suggestion i am new on android
Thanks
You can do this with package name using PackageManager.
try{
ApplicationInfo appInfo = getPackageManager()
.getApplicationInfo("com.abc.firstApp", 0 );
// application exists
} catch(NameNotFoundException nnfe ){
// application doesn't exist
}
replace com.abc.firstApp with your first app's package name.
You can't do this during install process. You have to check this from your second app. after finish installation of second app.

How to find the package name of default settings application

I want to prevent launching of task manager and Settings applications in my application. For this, I tried to obtain currently running application and checked whether their package name is allowed or not .If it is not allowed then show a new activity.
When work out it is show that the package name of default android Settings application is com.android.settings. Now I have some doubts
Is the Settings application has package name com.android.settings in all android versions? If not, which are they?
How to find package name of Task Manager?
try this
private String querySettingPkgName() {
Intent intent = new Intent(android.provider.Settings.ACTION_SETTINGS);
List<ResolveInfo> resolveInfos = getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
if (resolveInfos == null || resolveInfos.size() == 0) {
return "";
}
return resolveInfos.get(0).activityInfo.packageName;
}
For this,I tried to obtain currently running application and checked whether their package name is allowed or not .If it is not allowed then show a new activity.
Fortunately, for the users affected by your app, this will be unreliable.
Is the Settings application has package name com.android.settings in all android versions?
Not necessarily. More importantly, any given firmware can have any number of applications that modify settings, supplied by the firmware author. Some settings can be modified even without being part of the firmware, particularly on rooted devices.
If not,which are they?
You are welcome to make a list of all device manufacturers and ROM mod authors and ask them that question.
How to find package name of Task Manager?
There are any number of "task manager" apps included in devices, ROM mods, and available on the Play Store and other distribution points. You are welcome to make a list of all of them and ask their authors that question.
shell into the device using adb, and invoke:
pm list packages
this will provide you a list of pacakges. from there you will should see:
com.android.settings
final PackageManager pm = getPackageManager();
List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);
for (ApplicationInfo packageInfo : packages) {
Log.d("Packages", "" + packageInfo.packageName);
}
above code should help you
It's not totally clear what is the scenario.
I guess it is something along the lines of showing off devices to public but not have them f'up the device for others.
Maybe it would be better to do a whitelist instead of a blacklist. Meaning the shop should state which apps should be testable on the devices and then you start your activity if it is any other.
But this again will need maintenance: package names of popular apps may also change. You better provide a way of updating the settings of your app via an online service so you can change the needed packages without physical access to the devices and without having to download and install the complete app.
If you just need a device that goes through many hands and should not be tempered with I suggest using a modified device. I only know of Sonim: they provide a library (needs a Sonim provided hash key in your manifest to use that). With it you can prohibit the altering of many settings without preventing access to the whole settings app.

Categories

Resources