How can i get the name of the app from the package name. I don't want the name of my app. Here is my code:
String packName ="com.apphousebd.ramadan2017";
final PackageManager pm = getApplicationContext().getPackageManager();
ApplicationInfo ai;
try {
ai = pm.getApplicationInfo( packName, 0);
} catch (final NameNotFoundException e) {
ai = null;
}
final String applicationName = (String) (ai != null ? pm.getApplicationLabel(ai) : "(unknown)");
Related
I'm working on an android application that listens for notifications. Is there any way to get the name of the app which created the notification.
You need to get the package name first on receiving the new notification from the notification service::
#Override
public void onNotificationPosted(StatusBarNotification sbn) {
String pack = sbn.getPackageName();
String ticker = new String();
if(sbn.getNotification().tickerText != null) {
ticker = sbn.getNotification().tickerText.toString();
}
Bundle extras = sbn.getNotification().extras;
String title = extras.getString("android.title");
String text = extras.getCharSequence("android.text").toString();
int id1 = extras.getInt(Notification.EXTRA_SMALL_ICON);
Bitmap id = sbn.getNotification().largeIcon;
}
And then use the "pack" value to get the app name:
final PackageManager pm = getApplicationContext().getPackageManager();
ApplicationInfo ai;
try {
ai = pm.getApplicationInfo( pack, 0);
} catch (final PackageManager.NameNotFoundException e) {
ai = null;
}
final String applicationName = (String) (ai != null ?
pm.getApplicationLabel(ai) : "(unknown)");
I Already know how to get the package names of applications installed by the user.
But i cannot figure out how to get the package names of applications that are preinstalled or are system-ware like (Samsung Fit, Calender etc.) ??
List<ApplicationInfo> packages = context.getPackageManager().getInstalledApplications(PackageManager.GET_META_DATA);
for (ApplicationInfo packageInfo : packages) {
Log.d(TAG, "Package :" + packageInfo.packageName);
Log.d(TAG, "Launcher Activity :" + pm.getLaunchIntentForPackage(packageInfo.packageName));
}
ApplicationInfo is having a packageName field which provides you package name of the application.
You can use below code to get the list of system application PackageInfo,
public static ArrayList<PackageInfo> getSystemPackageInfos(final Activity context) {
ArrayList<PackageInfo> list = new ArrayList<PackageInfo>();
ArrayList<String> packageNames = new ArrayList<String>();
PackageManager pm = context.getPackageManager();
List<PackageInfo> pinfoList = pm.getInstalledPackages(0);
Collections.sort(pinfoList, PackageNameComparator);
for (PackageInfo pinfo : pinfoList) {
packageNames.add(pinfo.packageName);
boolean isSystem = false;
if (((pinfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0)) {
isSystem = false;
} else {
isSystem = true;
}
if (pinfo.applicationInfo.sourceDir.startsWith("/data/app/") && isSystem) {
//Non-system app
isSystem = false;
}
if (!isSystem) {
continue;
}
Bitmap icon = null;
Drawable apkIcon = getApplicationIcon(pinfo.applicationInfo, context);
try {
icon = Bitmap.createBitmap(apkIcon.getIntrinsicWidth(), apkIcon.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(icon);
apkIcon.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
apkIcon.draw(canvas);
} catch (ClassCastException e) {
}
String name = pinfo.applicationInfo.loadLabel(pm).toString();
long apkSize = new File(pinfo.applicationInfo.sourceDir).length();
list.add(pinfo);
}
return list;
}
I want to add an option in my file manager to show the App Icons of a directory. The code below didn't work; what did I do wrong?
ImageView icon;
private static Activity activity;
String temp = mFileMang.getCurrentDir();
} else if (sub_ext.equalsIgnoreCase("apk")) {
final Drawable appicon;
try {
PackageInfo packageInfo = activity.getPackageManager()
.getPackageArchiveInfo(temp,
PackageManager.GET_ACTIVITIES);
ApplicationInfo appInfo = packageInfo.applicationInfo;
appInfo.sourceDir = temp;
appInfo.publicSourceDir = temp;
appicon = appInfo
.loadIcon(activity.getPackageManager());
mViewHolder.icon.setImageDrawable(appicon);
} catch (Exception e) {
mViewHolder.icon.setImageResource(R.drawable.appicon);
}
try this.. i fetch the icon from the sd card directory ..icon from the apk files which are not installed ...
public class A extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.app_listing);
ListView list = (ListView) findViewById(R.id.app_listing);
ArrayList<PackageInfoStruct> listData = getApks();
list.setAdapter(new TestAdapter(listData, A.this));
}
class PackageInfoStruct {
String appname = "";
String pname = "";
String versionName = "";
int versionCode = 0;
Drawable icon;
String datadir = "";
}
public ArrayList<PackageInfoStruct> res;
private ArrayList<PackageInfoStruct> getApks() {
try {
String path = Environment.getExternalStorageDirectory() + "/test";
File file = new File(path);
String[] list = file.list();
res = new ArrayList<PackageInfoStruct>();
for (String str : list) {
String not_installed_apk_file = path + "/" + str;
PackageManager pm = getPackageManager();
PackageInfo pi = pm.getPackageArchiveInfo(
not_installed_apk_file, 0);
if (pi == null)
continue;
// the secret are these two lines....
pi.applicationInfo.sourceDir = not_installed_apk_file;
pi.applicationInfo.publicSourceDir = not_installed_apk_file;
//
Drawable APKicon = pi.applicationInfo.loadIcon(pm);
String AppName = (String) pi.applicationInfo.loadLabel(pm);
PackageInfoStruct pack = new PackageInfoStruct();
pack.icon = APKicon;
pack.pname = AppName;
res.add(pack);
}
} catch (Exception e) {
e.printStackTrace();
}
return res;
}
private ArrayList<PackageInfoStruct> getInstalledApps() {
try {
res = new ArrayList<PackageInfoStruct>();
List<PackageInfo> packs = getPackageManager().getInstalledPackages(
0);
for (int i = 0; i < packs.size(); i++) {
PackageInfo p = packs.get(i);
PackageInfoStruct newInfo = new PackageInfoStruct();
newInfo.appname = p.applicationInfo.loadLabel(
getPackageManager()).toString();
newInfo.pname = p.packageName;
newInfo.datadir = p.applicationInfo.dataDir;
newInfo.versionName = p.versionName;
newInfo.versionCode = p.versionCode;
newInfo.icon = p.applicationInfo.loadIcon(this
.getPackageManager());
res.add(newInfo);
}
} catch (Exception e) {
e.printStackTrace();
}
return res;
}
}
PackageManager..getPackageArchiveInfo(..) method will work only for installed apks, means it will operate only on an apk file, which has already been installed. Currently you maybe checking this on an apk file, which might not be installed and thus getting package info is failed.
What you can do is:
Unzip the apk file programatically (Basically apk file is a zip file, and you can extract it programatically. see http://www.jondev.net/articles/Unzipping_Files_with_Android_(Programmatically) )
Get the AndroidManifest.xml file from the zip entry
Parse the AndroidManifest.xml file ( see How to parse the AndroidManifest.xml file inside an .apk package )
Get the xml attribute 'android:icon' from it
Read the icon file from the zip entry again as bitmap.
use this icon bitmap wherever you want.
Just Try with this-
ImageView icon;
private static Activity activity;
String temp = mFileMang.getCurrentDir();
} else if (sub_ext.equalsIgnoreCase("apk")) {
final Drawable appicon;
try {
PackageInfo packageInfo = activity.getPackageManager()
.getPackageArchiveInfo(temp,
PackageManager.GET_ACTIVITIES);
ApplicationInfo appInfo = packageInfo.applicationInfo;
appInfo.sourceDir = temp;
appInfo.publicSourceDir = temp;
PackageManager pm = getPackageManager();
appicon = pm.getApplicationIcon(appInfo.packageName);
mViewHolder.icon.setImageDrawable(appicon);
} catch (Exception e) {
mViewHolder.icon.setImageResource(R.drawable.appicon);
}
to add this http://stackoverflow.com/questions/17919151/android-app-icon-filemanager/17924795#17924795 to my code i need to cut it. finally i got this:
but when I open the directory it load only 1 icon and show it for all other apps too.
https://www.dropbox.com/s/e2bonh3fkfseggf/Screenshot_2013-07-31-13-58-18.png
File file = new File(temp + "/" + mDataSource.get(position));
} else if (sub_ext.equalsIgnoreCase("apk")) {
try {
Drawable icon = getApk(file);
mViewHolder.icon.setImageDrawable(icon);
} catch (Exception e) {
mViewHolder.icon.setImageResource(R.drawable.appicon);
}
private Drawable getApk(File file2) {
try {
String path = mFileMang.getCurrentDir();
File file = new File(path);
String[] list = file.list();
for (String str : list) {
String not_installed_apk_file = path + "/" + str;
PackageManager pm = mContext.getPackageManager();
PackageInfo pi = pm.getPackageArchiveInfo(
not_installed_apk_file, 0);
if (pi == null)
continue;
// the secret are these two lines....
pi.applicationInfo.sourceDir = not_installed_apk_file;
pi.applicationInfo.publicSourceDir = not_installed_apk_file;
//
res = pi.applicationInfo.loadIcon(pm);
}
} catch (Exception e) {
e.printStackTrace();
}
return res;
}
PackageManager pm = context.getPackageManager();
PackageInfo info =pm.getPackageArchiveInfo(apkPath,PackageManager.GET_ACTIVITIES);
Below here is my code but i am getting default android launcher icon for all running applications:
PackageManager pm = getPackageManager();
ActivityManager am1 = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> processes = am1.getRunningTasks(Integer.MAX_VALUE);
if (processes != null) {
for (int k = 0; k < processes.size(); k++) {
// String pkgName = app.getPackageName();
String packageName = processes.get(k).topActivity
.getPackageName();
Drawable ico = null;
try
{
String pName = (String) pm.getApplicationLabel(pm
.getApplicationInfo(packageName,
PackageManager.GET_META_DATA));
ico = pm.getApplicationIcon(pName);
}
catch (NameNotFoundException e)
{
Log.e("ERROR", "Unable to find icon for package '"
+ packageName + "': " + e.getMessage());
}
icons.put(processes.get(k).topActivity.getPackageName(),ico);
}
just replace this line
ico = pm.getApplicationIcon(pName);
to this
ico = getApplicationInfo().loadIcon(getPackageManager());
EDITED full code :
public void getAllICONS() {
PackageManager pm = getPackageManager();
ActivityManager am1 = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> processes = am1
.getRunningTasks(Integer.MAX_VALUE);
if (processes != null) {
for (int k = 0; k < 5; k++) {
// String pkgName = app.getPackageName();
String packageName = processes.get(k).topActivity
.getPackageName();
Log.e("packageName-->", "" + packageName);
Drawable ico = null;
try {
String pName = (String) pm.getApplicationLabel(pm
.getApplicationInfo(packageName,
PackageManager.GET_META_DATA));
name.add("" + pName);
ApplicationInfo a = pm.getApplicationInfo(packageName,
PackageManager.GET_META_DATA);
ico = getPackageManager().getApplicationIcon(
processes.get(k).topActivity.getPackageName());
getPackageManager();
Log.e("ico-->", "" + ico);
} catch (NameNotFoundException e) {
Log.e("ERROR", "Unable to find icon for package '"
+ packageName + "': " + e.getMessage());
}
// icons.put(processes.get(k).topActivity.getPackageName(),ico);
icons.add(ico);
}
}
}
above code is show you icons like:
To retrieve the app icon, you can use the getApplicationIcon() method of the PackageManger.
In your case, since you are retriving the icon of the running apps, you can do something like:
final ActivityManager am = (ActivityManager) getSystemService(Activity.ACTIVITY_SERVICE);
final PackageManager pm = getPackageManager();
List<ActivityManager.RunningTaskInfo> runningTasks;
try {
runningTasks = am.getRunningTasks(100);
}
catch ( SecurityException e ) {
runningTasks = new ArrayList<ActivityManager.RunningTaskInfo>();
//e.printStackTrace();
}
Drawable icon;
for ( ActivityManager.RunningTaskInfo task : runningTasks ) {
final String packageName = task.topActivity.getPackageName();
try {
icon = pm.getApplicationIcon(packageName);
}
catch ( PackageManager.NameNotFoundException e ) {
//e.printStackTrace();
}
}
Otherwise, even better, you may use the other implementation of getApplicationIcon(), thus:
ApplicationInfo ai;
try {
ai = pm.getApplicationInfo(packageName, PackageManager.GET_META_DATA);
} catch ( PackageManager.NameNotFoundException e ) {
ai = null;
//e.printStackTrace();
}
if ( ai != null ) {
icon = pm.getApplicationIcon(ai);
}
PS: In any case, please note that getRunningTasks() will never return null, but an empty list at most.
How can I check that? Do i have to use PackageManager? Thank you :D
I check it this way (check if the returned value is unequal to null):
public static Intent findFacebookClient(Context con)
{
final String[] FacebookApps = {"com.facebook.android", "com.facebook.katana"};
Intent facebookIntent = new Intent();
facebookIntent.setType("text/plain");
final PackageManager packageManager = con.getPackageManager();
List<ResolveInfo> list = packageManager.queryIntentActivities(facebookIntent, PackageManager.MATCH_DEFAULT_ONLY);
for (int i = 0; i < FacebookApps.length; i++)
{
for (ResolveInfo resolveInfo : list)
{
String p = resolveInfo.activityInfo.packageName;
if (p != null && p.startsWith(FacebookApps[i]))
{
facebookIntent.setPackage(p);
return facebookIntent;
}
}
}
return null;
}
final PackageManager pm = getPackageManager();
List<ApplicationInfo> packages = pm
.getInstalledApplications(PackageManager.GET_META_DATA);
for (ApplicationInfo packageInfo : packages) {
Log.d(TAG, "Installed package : " + packageInfo.packageName);
}
Then you can check if packageInfo.packageName is equal to some string which contains the package name of that application.