Is it possible to get application name when package is removed - android

This is part of my receiver class:
Uri dataURI = intent.getData();
String appPackage = (dataURI != null ? dataURI.getSchemeSpecificPart() : null);
if (action.equals(Intent.ACTION_PACKAGE_ADDED)) {
UpdateUserGamesService.ACTION_PACKAGE_ADDED;
}
else if (action.equals(Intent.ACTION_PACKAGE_REMOVED)) {
UpdateUserGamesService.ACTION_PACKAGE_REMOVED;
}
Its working well. Is there a way to get the application name and not only the package name?

You can try ApplicationInfo class :
Information you can retrieve about a particular application. This corresponds to information collected from the AndroidManifest.xml's tag.
Try this code to get third party application name :
List<ApplicationInfo> apps = getPackageManager().getInstalledApplications(0);
for (int i=0; i < apps.size(); i++)
{
if ((apps.get(i).flags & ApplicationInfo.FLAG_SYSTEM) == 1)
{
String appName = (String) getPackageManager().getApplicationLabel(apps.get(i));
Log.i("App Name", appName);
}
}
Now your question is :
Is it possible to get application name when package is removed ?
Well i will suggest that get all application name and store it using Persistent Storage (SqLite will be best).
In future if any one removes package still you will have all information related to that application.
I hope this will help.

Related

Extracting the package name of the last installed app on my phone

I am writing a code to extract the package name, application name, and icon from the last installed app on my phone. I can get the application common name and icon from the application info, but I can't seem to figure out how to get the package name. All the codes that I have found to get the package name give me the package name of MY app, not the last installed app.
It seems like I need to find a method to get the package name, where I can pass in the application info as the parameter (like I do for the application common name and icon).
final PackageManager pm = context.getPackageManager();
ApplicationInfo ai;
try {
ai = pm.getApplicationInfo(intent.getData().getSchemeSpecificPart(), 0);
Log.d("tag_name","Application Info" + ai);
PACKAGE_NAME = context.getApplicationContext().getPackageName();
Log.d("tag_name","Package Name" + PACKAGE_NAME);
} catch (final PackageManager.NameNotFoundException e) {
ai = null;
}
final String applicationName = (String) (ai != null ? pm.getApplicationLabel(ai) : "(unknown)");
Log.d("tag_name", "Application NAME" + applicationName);
// http://www.carbonrider.com/2016/01/01/extract-app-icon-in-android/
try {
Drawable icon = context.getPackageManager().getApplicationIcon(ai);
Log.d("tag_name", "ICON" + icon);}
catch (Exception e){}
Firstly receive all apps with this Code :
List<PackageInfo> packages = packageManager.getInstalledPackages(PackageManager.GET_META_DATA);
Then sort the packages list with this code :
Collections.sort(packages, new Comparator<PackageInfo>() {
#Override
public int compare(PackageInfo p1, PackageInfo p2) {
return Long.toString(p2.firstInstallTime).compareTo(Long.toString(p1.firstInstallTime));
}
});
Then you can receive the package Name of the latest installed app this way:
packages.get(0).packageName

Correct name of installed applications

I am getting the names for my installed applications with below code and use them to see if any updates for this application is available.
But sometimes an incorrect name (MX Speler instead of MX Player) is being provided, as a result no updates are found.
Is there any better code i can/should use?
{
final PackageInfo pi = installedInfo != null ? installedInfo : downloadedInfo;
final PackageManager pm = getApplicationContext().getPackageManager();
ApplicationInfo ai;
try {
ai = pm.getApplicationInfo(pi ??, 0); //How to set the name of the installed application?
} catch (final NameNotFoundException e) {
ai = null;
}
final String applicationName = (String) (ai != null ? pm.getApplicationLabel(ai) : "(unknown)");
System.out.println("Application name : "+ applicationName);
}
You should not use labels as it may be different for every language or changed on each update. You should only rely on application's id (packageId) as this id stays unchanged for the whole life of the application.

Intent to get the UID of application uninstalled

i've a receiver that is fired on any application uninstall. I want to get the UID of the application . Currently i got the package name that was uninstalled but when i'm trying to get the UID, its returning null. Currently, i'm getting the UID of any package from following code.
public String getID(String pckg_name) {
ApplicationInfo ai = null;
String id = "";
try {
ai = pm.getApplicationInfo(pckg_name, 0);
id = "" + ai.uid;
} catch (final NameNotFoundException e) {
id = "";
}
return id;
}
You can't get the UID after the package has been uninstalled because it is no longer there. The broadcast Intent is sent after the package has been removed. However...
...from the documentation:
The broadcast Intent that is broadcast when the application is removed (uninstalled) contains an extra EXTRA_UID containing the integer uid previously assigned to the package.

What is android:sharedUserLabel and what added value does it add on top of android:sharedUserID?

The documentation (http://developer.android.com/guide/topics/manifest/manifest-element.html#uid) only states I can't use raw strings and the API level it was added, but doesn't explain why I would want to use it.
If I already set android:sharedUserID to "com.foo.bar" what value should I put in the string referenced by android:sharedUserLabel, and most importantly why!?
Thank you
As far as I understand from the AOSP actually you can use this label just to display a pretty name to a user (if you have several processes in the same uid). For instance, here is a part of code in the RunningState.java file:
// If we couldn't get information about the overall
// process, try to find something about the uid.
String[] pkgs = pm.getPackagesForUid(mUid);
// If there is one package with this uid, that is what we want.
if (pkgs.length == 1) {
try {
ApplicationInfo ai = pm.getApplicationInfo(pkgs[0], 0);
mDisplayLabel = ai.loadLabel(pm);
mLabel = mDisplayLabel.toString();
mPackageInfo = ai;
return;
} catch (PackageManager.NameNotFoundException e) {
}
}
// If there are multiple, see if one gives us the official name
// for this uid.
for (String name : pkgs) {
try {
PackageInfo pi = pm.getPackageInfo(name, 0);
if (pi.sharedUserLabel != 0) {
CharSequence nm = pm.getText(name,
pi.sharedUserLabel, pi.applicationInfo);
if (nm != null) {
mDisplayLabel = nm;
mLabel = nm.toString();
mPackageInfo = pi.applicationInfo;
return;
}
}
} catch (PackageManager.NameNotFoundException e) {
}
}
Basically, it does the following things. At first, it tries to get information about the overall process. If it has not find, it tries to get information using UID of the application as a parameter (this is a part of code that I've given here). If there is only one package with this UID the information about the process is got from this package. But if there are several packages (using shareUserId) then it iterates and tries to find official (pretty) name.
As a confirmation to my words I found the following string in MediaProvider:
<!-- Label to show to user for all apps using this UID. -->
<string name="uid_label">Media</string>
Thus, all process that uses android:sharedUserId="android.media" will have name Media.
I do not think that this feature will be used a lot by ordinary developers and is useful for them.

How to get the name of the application in android?

I want to get the name of my application. How can i get that?
Thanks in advance.
You can use PackageItemInfo -> nonLocalizedLabel to get application name.
val applicationName = context.applicationInfo.nonLocalizedLabel.toString()
[Old Answer]
Usually, we do add the application name with app_name string resource, so you can write below code to access the app name
String applicationName = getResources().getString(R.string.app_name);
Reference : Resource Types
But note that, this resource name is not mandatory and can be changed to any other string name. In that case, the code will not work. See the first code snippet which uses PackageItemInfo -> nonLocalizedLabel above for a better solution.
You can use PackageManager class to obtain ApplicationInfo:
final PackageManager pm = context.getPackageManager();
ApplicationInfo ai;
try {
ai = pm.getApplicationInfo(packageName, 0);
} catch (final NameNotFoundException e) {
ai = null;
}
final String applicationName = (String) (ai != null ? pm.getApplicationLabel(ai) : "(unknown)");
EDIT: CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(info.processName,PackageManager.GET_META_DATA));
This would return the application name as defined in <application> tag of its manifest.
you can use PackageManager#getApplicationInfo()
For getting the Application Name for all packages installed in the device.
Assuming you have your current Context object ctx
Resources appR = ctx.getResources();
CharSequence txt = appR.getText(appR.getIdentifier("app_name",
"string", ctx.getPackageName()));
Context has function getString(int resId): Context
so you can use it easily like this.
context.getString(R.string.app_name);

Categories

Resources