how to get running applications icon? - android

**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);
}
}
}

Related

How can I get the list of all device admins (active as well as inactive ) in android?

The Google API allows us to get the list of all active admins in the device from the method: getActiveAdmins(). However, my requirement is that I want the list of all possible admins in the device , whether active or not. Is there some method to do so ?
Thanks
You can find list of applications by below code
final Intent deviceAdminIntent = new Intent("android.app.action.DEVICE_ADMIN_ENABLED", null);
final List<ResolveInfo> pkgAppsList = getPackageManager().queryBroadcastReceivers(deviceAdminIntent, 0);
for (ResolveInfo aResolveInfo : pkgAppsList) {
String pkg = aResolveInfo.activityInfo.applicationInfo.packageName;
String name = aResolveInfo.activityInfo.applicationInfo.loadLabel(getPackageManager()).toString();
System.out.println("Package :: " + pkg);
System.out.println("Name :: " + name);
}
You can get all the necessary data in the ResolveInfo of an application. You can check ResolveInfo javadoc here.
I wrote a method which returns the ComponentName as same as getActiveAdmins
private List<ComponentName> getAllAdmins(Context mContext) {
List<ComponentName> result = new ArrayList<ComponentName>();
// Read all receivers who can listen android.app.action.DEVICE_ADMIN_ENABLED
// You can add all other action which can be used for DeviceAdminReceiver
final Intent deviceAdminIntent = new Intent("android.app.action.DEVICE_ADMIN_ENABLED", null);
final List<ResolveInfo> pkgAppsList = mContext.getPackageManager().queryBroadcastReceivers(deviceAdminIntent, 0);
for (ResolveInfo aResolveInfo : pkgAppsList) {
// Prepare component and add to list
result.add(new ComponentName(aResolveInfo.activityInfo.applicationInfo.packageName,
aResolveInfo.activityInfo.name));
//String pkg = aResolveInfo.activityInfo.applicationInfo.packageName;
//String name = aResolveInfo.activityInfo.applicationInfo.loadLabel(getPackageManager()).toString();
//System.out.println("Package :: " + pkg);
//System.out.println("Name :: " + name);
}
return result;
}
You can find the necessary code in the following Android source for DeviceAdminSettings: https://android.googlesource.com/platform/packages/apps/Settings/+/kitkat-release/src/com/android/settings/DeviceAdminSettings.java
DevicePolicyManager mDPM; // = (DevicePolicyManager) getActivity().getSystemService(Context.DEVICE_POLICY_SERVICE);
final HashSet<ComponentName> mActiveAdmins = new HashSet<ComponentName>();
final ArrayList<DeviceAdminInfo> mAvailableAdmins = new ArrayList<DeviceAdminInfo>();
...
mActiveAdmins.clear();
List<ComponentName> cur = mDPM.getActiveAdmins();
if (cur != null) {
for (int i=0; i<cur.size(); i++) {
mActiveAdmins.add(cur.get(i));
}
}
mAvailableAdmins.clear();
List<ResolveInfo> avail = getActivity().getPackageManager().queryBroadcastReceivers(
new Intent(DeviceAdminReceiver.ACTION_DEVICE_ADMIN_ENABLED),
PackageManager.GET_META_DATA | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS);
if (avail == null) {
avail = Collections.emptyList();
}
// Some admins listed in mActiveAdmins may not have been found by the above query.
// We thus add them separately.
Set<ComponentName> activeAdminsNotInAvail = new HashSet<ComponentName>(mActiveAdmins);
for (ResolveInfo ri : avail) {
ComponentName riComponentName =
new ComponentName(ri.activityInfo.packageName, ri.activityInfo.name);
activeAdminsNotInAvail.remove(riComponentName);
}
if (!activeAdminsNotInAvail.isEmpty()) {
avail = new ArrayList<ResolveInfo>(avail);
PackageManager packageManager = getActivity().getPackageManager();
for (ComponentName unlistedActiveAdmin : activeAdminsNotInAvail) {
List<ResolveInfo> resolved = packageManager.queryBroadcastReceivers(
new Intent().setComponent(unlistedActiveAdmin),
PackageManager.GET_META_DATA
| PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS);
if (resolved != null) {
avail.addAll(resolved);
}
}
}
for (int i = 0, count = avail.size(); i < count; i++) {
ResolveInfo ri = avail.get(i);
try {
DeviceAdminInfo dpi = new DeviceAdminInfo(getActivity(), ri);
if (dpi.isVisible() || mActiveAdmins.contains(dpi.getComponent())) {
mAvailableAdmins.add(dpi);
}
} catch (XmlPullParserException e) {
Log.w(TAG, "Skipping " + ri.activityInfo, e);
} catch (IOException e) {
Log.w(TAG, "Skipping " + ri.activityInfo, e);
}
}
getListView().setAdapter(new PolicyListAdapter());
This is how Android lists them for you in the Device Admins tab.

how to get running applications icon on android programmatically

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 to get app package name by pid?

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;
}

How to get the list of installed application from google play store in android?

i want the application list that is installed from google play or from any external storage, but not the pre installed system application.
i just use below code.....
class PInfo {
private String appname = "";
private String pname = "";
private String versionName = "";
private int versionCode = 0;
private Drawable icon;
private void prettyPrint() {
Log.v("", "*** appname = " + appname + " *** \n" + " packagename = " + pname + "\n" + " version name = " + versionName + "\n" + " version code = " + versionCode + " \n\n\n\n");
}
}
private ArrayList<PInfo> getPackages() {
ArrayList<PInfo> apps = getInstalledApps(true); /* false = no system packages */
final int max = apps.size();
int i = 0;
for (i=0; i<max; i++) {
apps.get(i).prettyPrint();
}
Log.v(TAG, "Total APPs = "+i);
return apps;
}
private ArrayList<PInfo> getInstalledApps(boolean getSysPackages) {
ArrayList<PInfo> res = new ArrayList<PInfo>();
List<PackageInfo> packs = getPackageManager().getInstalledPackages(PackageManager.GET_META_DATA);
for(int i=0;i<packs.size();i++) {
PackageInfo p = packs.get(i);
if ((!getSysPackages) && (p.versionName == null)) {
continue ;
}
PInfo newInfo = new PInfo();
newInfo.appname = p.applicationInfo.loadLabel(getPackageManager()).toString();
newInfo.pname = p.packageName;
newInfo.versionName = p.versionName;
newInfo.versionCode = p.versionCode;
newInfo.icon = p.applicationInfo.loadIcon(getPackageManager());
res.add(newInfo);
}
return res;
}
but this code gives me information of system application also.....
can anybody know how to get only installed application except the installed system application ?
thanks....
To discover if your application is a system application you can use the following code:
PackageManager pm = getPackageManager();
List<ApplicationInfo> installedApps = pm.getInstalledApplications(0);
for (ApplicationInfo aInfo: installedApps) {
if ((aInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
// system application
} else {
//user application
}
}
If you need to discover if an application is installed from a market you need to use the following method of Package: getInstallerPackageName. It will return the packageName of the application that installed your application.
public class AppList extends Activity {
private ListView lView;
private ArrayList results = new ArrayList();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lView = (ListView) findViewById(R.id.list1);
PackageManager pm = this.getPackageManager();
Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
List list = pm.queryIntentActivities(intent, PackageManager.PERMISSION_GRANTED);
for (ResolveInfo rInfo : list) {
results.add(rInfo.activityInfo.applicationInfo
.loadLabel(pm).toString());
Log.w("Installed Applications", rInfo.activityInfo.applicationInfo
.loadLabel(pm).toString());
}
lView.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, results));
}
}

check if there are 2 instances running

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!!

Categories

Resources