Packagename of Installed apps - android

I want to work on an app similar to Package Name Viewer 2.0 that could load the packagenames of all the installed apps in the device. Actually I have searched a lot about it even discussed with my seniors and even tried to contact with the owner of Package Name Viewer 2.0but got no answer. I am not asking to write the whole code just give a favor that how can i achieve this.
In short: I want to add the code in onCreate() that whenever app starts it should load all the packagenames of installed apps.
Related Question: How to get package name from anywhere?

Use PackageManager to retrieve ApplicationInfo of all apps.
public static List<App> getAllAppInstalled(Context context) {
PackageManager packageManager = context.getPackageManager();
List<App> apps = new ArrayList<>();
List<ApplicationInfo> packages = packageManager.getInstalledApplications(PackageManager.GET_META_DATA);
for (ApplicationInfo packageInfo : packages) {
String applicationName = ((packageInfo != null) ? packageManager.getApplicationLabel(packageInfo) : "Unknown").toString();
if (packageInfo.enabled) {
apps.add(new App(applicationName, packageInfo.packageName));
}
}
return apps;
}
App class should look like:
public class App {
private String appName;
private String appPackage;
public App(String appName, String appPackage) {
this.appName = appName;
this.appPackage = appPackage;
}
//getters and setters
}

Related

Android get Package name from Activity

I have an activity name from a third party application
"com.example.packagename.MainActivity"
How can I retrieve programmatically the package name of this third party application?
PS: I am not even sure this is an Activity. It could be a wrong string.
You can find the package name from an external app:
private String getExternalPackageName(String appname) {
PackageManager pm = thisActivity.getPackageManager();
List<ApplicationInfo> aList = pm.getInstalledApplications(PackageManager.GET_META_DATA);
String aName = "";
for (ApplicationInfo appi : aList) {
String n = (String)pm.getApplicationLabel(appi);
if (n.toUpperCase().contains(appname)) {
aName = appi.packageName;
}
}
return aName;
}
be sure to call this function with appname => uppercase

How to check if an android application was installed even if it is removed from the device

Is there a way to check whether an android application was installed in the device through another android app, even if the user has removed it from his/her android device
For eg:-
Let us suppose a user has installed android app A and removed it from his/her device. Now I have installed an Android app B.Is there a way to check through application B, if the user had installed app A
please try this method its return whether application is install or not but just you need to pass package name of your app.
private boolean isPackageInstalled(String packagename, Context context) {
PackageManager pm = context.getPackageManager();
try {
pm.getPackageInfo(packagename, PackageManager.GET_ACTIVITIES);
return true;
} catch (NameNotFoundException e) {
return false;
}
}
Try this...
final PackageManager packageManager = getPackageManager();
List<ApplicationInfo> AppPackages = packageManager.getInstalledApplications(PackageManager.GET_META_DATA);
for (ApplicationInfo AppPackageInfo : AppPackages) {
Log.d(TAG, "Installed App Package :" + AppPackageInfo.packageName);
Log.d(TAG, "Launch Activity :" + packageManager.getLaunchIntentForPackage(AppPackageInfo.packageName));
}
UPDATED
private boolean isAppInstalled(String appPackage) {
boolean isAppExists = false;
PackageManager packageManager = getPackageManager();
List<ApplicationInfo> AppPackages = packageManager.getInstalledApplications(PackageManager.GET_META_DATA);
for (ApplicationInfo appPackageInfo : AppPackages) {
if (appPackageInfo.packageName.equalsIgnoreCase(appPackage)) {
isAppExists = true;
break;
} else {
isAppExists = false;
}
}
return isAppExists;
}
Here you need to call the method with app package name(the app which you wants to know)

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

Custom Keyboard Android get current App

I wish to get name/packageName of current app using my customKeyboard service.
Is there anyway to do so?
You have to rely on getCurrentInputEditorInfo(), a method in InputMethodService, to achieve this:
String packageName = getCurrentInputEditorInfo().packageName;
Found answer in this topic: How to check current running applications in Android?
If app is running its obviosuly the one calling my keyboard.
private String getApplicationName() {
final PackageManager pm = mActivity.getApplicationContext()
.getPackageManager();
ApplicationInfo ai;
String appName;
try {
ai = pm.getApplicationInfo(mActivity.getPackageName(), 0);
appName = (String) pm.getApplicationLabel(ai);
} catch (final NameNotFoundException e) {
appName = "(unknown)";
}
return appName;
}
use this method to get Appname/Package name

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