get application name from package name
I want to get the application name from application package name. Somebody please show me how I can get this.
public class AppInstalledListener extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String action = intent.getAction();
if(action.equals("android.intent.action.PACKAGE_ADDED")){
Logger.debug("DATA:"+intent.getData().toString());
}
if(action.equals("android.intent.action.PACKAGE_REMOVED")){
Logger.debug("DATA:"+intent.getData().toString());
}
if(action.equals("android.intent.action.PACKAGE_REPLACED")){
Logger.debug("DATA:"+intent.getData().toString());
}
}
}
You can use PackageManager
class to obtain ApplicationInfo
:
final PackageManager pm = getApplicationContext().getPackageManager();
ApplicationInfo ai;
try {
ai = pm.getApplicationInfo( this.getPackageName(), 0);
} catch (final NameNotFoundException e) {
ai = null;
}
final String applicationName = (String) (ai != null ? pm.getApplicationLabel(ai) : "(unknown)");
This would return the application name as defined in <application>
tag of its manifest.
Try this
final String packageName = "my.application.package"
PackageManager packageManager= getApplicationContext().getPackageManager();
String appName = (String) packageManager.getApplicationLabel(packageManager.getApplicationInfo(packageName, PackageManager.GET_META_DATA));
and replace
packageName
with your package full name.
you can get packageName using mContext.getPackageName()
where mContext = yourActivityName.this
for Activity and getActivity() for fragment.
public static String getAppNameFromPkgName(Context context, String Packagename) {
try {
PackageManager packageManager = context.getPackageManager();
ApplicationInfo info = packageManager.getApplicationInfo(Packagename, PackageManager.GET_META_DATA);
String appName = (String) packageManager.getApplicationLabel(info);
return appName;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
return "";
}
}
It seems that you are able to receive the event of new package added after that its a very simple concept to get the all relevant information about that package like one such information is the application name so here is the concept
-> your device package manager has all the information related to it so just make an object of that it will give you all the information related with the package name.
-> You should also remember that the intent gives you "package: real_package_name" so first you have to get real name first by spilling(I used) or by any other simple implementation of String
-> Here is the code hope you will get what you want I m also giving information about how you can get app name,app icon,app version,app version code etc.....
public class NewAppReciver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals("android.intent.action.PACKAGE_ADDED")){
String[] a=intent.getData().toString().split(":");
String packageName=a[a.length-1];
List<PackageInfo> packageInfoList = context.getPackageManager().getInstalledPackages(0);
for (int i = 0; i < packageInfoList.size(); i++) {
PackageInfo packageInfo = packageInfoList.get(i);
if(packageInfo.packageName.equals(packageName)){
String appName = packageInfo.applicationInfo.loadLabel(context.getPackageManager()).toString();
String appVersion = packageInfo.versionName;
int appVerCode = packageInfo.versionCode;
Drawable app_icon = packageInfo.applicationInfo.loadIcon(context.getPackageManager());
}
}
}
}
}
But at the time of the application Uninstall you can only get the package name as on Un installation all other information gets removed by the system.