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.
Related
**i found thishow to get running applications icon on android programmatically on how we show icons and names of running application but idont know what the type of icons.add(ico);and name.add("" + pName); are so can some on help me what should i do to make it run **
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);
}
}
}
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);
In below given code I want to kill (forcestop) all the checked applications but after restarting my application it's showing the name of these killed applications again.
I really don't know if they're really killed or not. Why this is so?
kill.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
//Killing selected Apps
String savedapp;
int code = 0;
int count = lv.getAdapter().getCount();
for (int j = 0; j < count; j++)
{
if (lv.isItemChecked(j))
{
Intent ints = new Intent(Intent.ACTION_MAIN, null);
ints.addCategory(Intent.CATEGORY_LAUNCHER);
savedapp = lv.getItemAtPosition(j).toString();
nm.removeAll(nm);
// this.l = getListView();
// l.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
PackageManager pm = getApplicationContext().getPackageManager();
List<ResolveInfo> intentlist = pm.queryIntentActivities(ints, PackageManager.PERMISSION_GRANTED);
ActivityManager am1 = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> processes = am1.getRunningTasks(Integer.MAX_VALUE);
if (processes != null)
{
for (int i = 0; i < processes.size(); i++) {
String packageName = processes.get(i).topActivity
.getPackageName();
RunningTaskInfo temp = processes.get(i);
try
{
pName = (String) pm.getApplicationLabel(pm
.getApplicationInfo(packageName,
PackageManager.GET_META_DATA));
}
catch (NameNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
// int f = 0;
if (savedapp.equals(pName)) {
// finish(pm.);
code = intentlist.get(i).activityInfo
.hashCode();
finishActivity(code);
am1.killBackgroundProcesses(packageName);
am1.restartPackage(packageName);
android.os.Process.killProcess(temp.id);
finishActivity(temp.id);
// Toast.makeText(this, packageName,
// Toast.LENGTH_SHORT).show();
//f = 1;
}
else
{
nm.add(pName.trim());
}
}
}
}
}
}
};//kill button close
String callerPackage = getAppNameByPID(getContext(), Binder.getCallingPid());
private String getAppNameByPID(Context context, int callingPid) {
//How ?
....
}
my questions:
How to get app package name by pid?
Each process can hold multiple packages, so it looks like:
private String[] getPackageNames(int pid)
{
ActivityManager activityManager = (ActivityManager)getContext().getSystemService(Activity.ACTIVITY_SERVICE);
List<RunningAppProcessInfo> runningAppProcesses = activityManager.getRunningAppProcesses();
for(RunningAppProcessInfo runningAppProcessInfo : runningAppProcesses)
{
try
{
if(runningAppProcessInfo.pid == pid)
{
return runningAppProcessInfo.pkgList;
}
}
catch(Exception e)
{
}
}
return null;
}
Hello you can try out this code, it works fine for me.
private String getAppName(int pID)
{
String processName = "";
ActivityManager am = (ActivityManager)this.getSystemService(ACTIVITY_SERVICE);
List l = am.getRunningAppProcesses();
Iterator i = l.iterator();
PackageManager pm = this.getPackageManager();
while(i.hasNext())
{
ActivityManager.RunningAppProcessInfo info = (ActivityManager.RunningAppProcessInfo)(i.next());
try
{
if(info.pid == pID)
{
CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(info.processName, PackageManager.GET_META_DATA));
//Log.d("Process", "Id: "+ info.pid +" ProcessName: "+ info.processName +" Label: "+c.toString());
//processName = c.toString();
processName = info.processName;
}
}
catch(Exception e)
{
//Log.d("Process", "Error>> :"+ e.toString());
}
}
return processName;
}
I am trying to check if my app is running already if the user is trying to enter it again.
it is possible if one app Launched anther one and the user entering with a mobile to the second one, and will be 2 instance.
my launcher app that starts the original one looks like that :
Intent i = new Intent(Intent.ACTION_MAIN);
PackageManager manager = getPackageManager();
i = manager.getLaunchIntentForPackage("com.test.vayo");
i.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(i);
On my original code I am trying to check if there is 2 instance but always get true on this code
if (processName == null) {
return false;
}
ActivityManager manager = (ActivityManager) context.getSystemService(context.ACTIVITY_SERVICE);
List<RunningAppProcessInfo> processes = manager.getRunningAppProcesses();
for (RunningAppProcessInfo process : processes) {
if (processName.equals(process.processName)) {
return true;
}
}
return false;
the proccessName taken from this code, the pid is from android.os.Process.myPid():
private String getAppName(int pID)
{
String processName = "";
ActivityManager am = (ActivityManager)context.getSystemService(context.ACTIVITY_SERVICE);
List l = am.getRunningAppProcesses();
Iterator i = l.iterator();
PackageManager pm = context.getPackageManager();
while(i.hasNext())
{
ActivityManager.RunningAppProcessInfo info = (ActivityManager.RunningAppProcessInfo)(i.next());
try
{
if(info.pid == pID)
{
CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(info.processName, PackageManager.GET_META_DATA));
//Log.d("Process", "Id: "+ info.pid +" ProcessName: "+ info.processName +" Label: "+c.toString());
//processName = c.toString();
processName = info.processName;
}
}
catch(Exception e)
{
//Log.d("Process", "Error>> :"+ e.toString());
}
}
return processName;
}
hope you can tell whats wrong why I am getting always true on my first block of code.
thanks a lot!!