I'd like to load icon from a running task package name like this
com.android.smspush.WapPushManager
You can get top running application by following code : once you get applicationInfo, you can get icon from info.
ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
List l = am.getRecentTasks(1, ActivityManager.RECENT_WITH_EXCLUDED);
Iterator i = l.iterator();
PackageManager pm = this.getPackageManager();
while (i.hasNext()) {
ActivityManager.RunningAppProcessInfo info = (ActivityManager.RunningAppProcessInfo)(i.next());
try {
CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(
info.processName, PackageManager.GET_META_DATA));
Drawable ico = info.loadIcon(pm); // Icon of the application
Log.w("LABEL", c.toString());
} catch (Exception e) {
// Name Not FOund Exception
}
}
public class AppIconDrawable {
private HashMap<String, Drawable> drawables;
private static AppIconDrawable sharedInstance = null;
private AppIconDrawable(){
drawables = new HashMap<>();
}
public static AppIconDrawable getSharedInstance(){
if (sharedInstance == null)
sharedInstance = new AppIconDrawable();
return sharedInstance;
}
public void setDrawableForKey(String key, Drawable drawable){
drawables.put(key, drawable);
}
public Drawable getDrawableForKey(String key){
return drawables.get(key);
}
}
To save icon i used it, where rp.process = com.android.smspush.WapPushManager
PackageManager manager = getActivity().getPackageManager();
List<ActivityManager.RunningAppProcessInfo> listProcesses = manager.getRunningAppProcesses();
for (ActivityManager.RunningAppProcessInfo info : listProcesses) {
try {
ApplicationInfo appinfo = manager.getApplicationInfo(info.processName, PackageManager.GET_META_DATA);
AppIconDrawable.getSharedInstance().setDrawableForKey(info.processName, manager.getApplicationIcon(appinfo));
} catch (PackageManager.NameNotFoundException e) {
AppIconDrawable.getSharedInstance().setDrawableForKey(info.processName, context.getResources().getDrawable(R.drawable.ic_android_default));
}
}
To get icon i used it, where rp.process = com.android.smspush.WapPushManager
holder.imgApp.setImageDrawable(AppIconDrawable.getSharedInstance().getDrawableForKey(rp.process));
Related
I'm loading all apps from android and then I display them in my launcher (icon + name). Code looks like following:
public class PhoneAppItem
{
String mPackageName = null;
String mActivityName = null;
String mName = null;
ActivityInfo mActivityInfo = null;
public PhoneAppItem(String packageName, String activityName)
{
mPackageName = packageName;
mActivityName = activityName;
}
public void loadInfo()
{
PackageManager pm = MainApp.get().getPackageManager();
try
{
// following line throws the exception!
mActivityInfo = pm.getActivityInfo(new ComponentName(mPackageName, mActivityName), 0);
// some other code...
}
catch (PackageManager.NameNotFoundException e)
{
L.e(e);
mName = mPackageName;
}
catch (NullPointerException e)
{
L.e(e);
mName = mPackageName;
}
}
}
Package names and activity names for all my PhoneAppItem items are retrieved like following (for android < 5 which is relevant for the only yet known device having this issue):
List<PhoneAppItem> apps = new ArrayList<>();
ActivityManager activityManager = (ActivityManager) MainApp.get().getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> processInfos = activityManager.getRunningAppProcesses();
for (ActivityManager.RunningAppProcessInfo processInfo : processInfos)
{
try
{
ApplicationInfo ai = pm.getApplicationInfo(processInfo.processName, 0);
if (!excludedPackages.contains(ai.packageName))
apps.add(new PhoneAppItem(ai.packageName, ai.className));
}
catch (PackageManager.NameNotFoundException e) {}
}
I now have a user that get's following exception:
android.content.pm.PackageManager$NameNotFoundException: ComponentInfo{com.package.myapp/com.package.myapp.app.MainApp}
at android.app.ApplicationPackageManager.getActivityInfo(ApplicationPackageManager.java:262)
at com.package.myap.classes.PhoneAppItem.h(PhoneAppItem.java:70)
...
Observations
the exception above is thrown for all apps, even for my own app
until now, this behaviour is unique to one user using following hardware: Lenovo Yoga Tablet HD+ (b8080), Android 4.4.2
Question
Does anyone have an idea why this could happen?
If processInfo.processName does not work, try another method of getting package:
processInfo.pkgList[0]
I want to find from within my broadcast receiver what other activities are currently running. This is the code that i use from an activity to find the other running activites but when i try to use this code in my broadcast receiver i get errors on the following lines:
ActivityManager am = (ActivityManager)this.getSystemService(ACTIVITY_SERVICE);
shows the error in eclipse ACTIVITY_SERVICE cannot be resolved to a variable
PackageManager pm = this.getPackageManager();
and this shows the error in eclipse The method getPackageManager() is undefined for the type ScreenReceiver (my broadcast receiver)
Here is the full code:
public void getRunning(){
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 {
CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(info.processName, PackageManager.GET_META_DATA));
Log.w("LABEL", c.toString());
runningApplications.add(c.toString());
}catch(Exception e) {
//Name Not Found Exception
}
}
}
Since BroadcastReceiver does not descent from a Context, you cannot use this, as you can do in Activity. You should use the instance of Context that is passed to your onReceive() method.
public void getRunning(Context context){
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 {
CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(info.processName, PackageManager.GET_META_DATA));
Log.w("LABEL", c.toString());
runningApplications.add(c.toString());
}catch(Exception e) {
//Name Not Found Exception
}
}
}
try as:
public class ScreenReceiver extends BroadcastReceiver {
private Context ctext;
#Override
public void onReceive(Context context, Intent intent) {
ctext=context;
//OR you can also pass context as param to getRunning()
//your code here....
}
public void getRunning(){
ActivityManager am = (ActivityManager)ctext.getSystemService(Context.ACTIVITY_SERVICE);
List l = am.getRunningAppProcesses();
Iterator i = l.iterator();
PackageManager pm = ctext.getPackageManager();
while(i.hasNext()) {
ActivityManager.RunningAppProcessInfo info = (ActivityManager.RunningAppProcessInfo)(i.next());
try {
CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(info.processName, PackageManager.GET_META_DATA));
Log.w("LABEL", c.toString());
runningApplications.add(c.toString());
}catch(Exception e) {
//Name Not Found Exception
}
}
}
}
This question already has answers here:
Get active Application name in Android
(3 answers)
Closed 6 months ago.
Please suggest me how can I get current open application name, even if there is home screen on device then I will find "Home screen is open".
Use an AccessibilityService
You can detect the currently active window by using an AccessibilityService.
In the onAccessibilityEvent callback, check for the TYPE_WINDOW_STATE_CHANGED event type to determine when the current window changes.
Check if the window is an activity by calling PackageManager.getActivityInfo().
I tested and working in Android 2.2 (API 8) through Android 7.1 (API 25).
public class MyAccessibilityService extends AccessibilityService {
#Override
protected void onServiceConnected() {
super.onServiceConnected();
//Configure these here for compatibility with API 13 and below.
AccessibilityServiceInfo config = new AccessibilityServiceInfo();
config.eventTypes = AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED;
config.feedbackType = AccessibilityServiceInfo.FEEDBACK_GENERIC;
if (Build.VERSION.SDK_INT >= 16)
//Just in case this helps
config.flags = AccessibilityServiceInfo.FLAG_INCLUDE_NOT_IMPORTANT_VIEWS;
setServiceInfo(config);
}
#Override
public void onAccessibilityEvent(AccessibilityEvent accessibilityEvent) {
Log.d("ABC-",accessibilityEvent.getPackageName()+" -- "+accessibilityEvent.getClassName());
if (accessibilityEvent.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
if (accessibilityEvent.getPackageName() != null && accessibilityEvent.getClassName() != null) {
ComponentName componentName = new ComponentName(
accessibilityEvent.getPackageName().toString(),
accessibilityEvent.getClassName().toString()
);
ActivityInfo activityInfo = tryGetActivity(componentName);
boolean isActivity = activityInfo != null;
if (isActivity)
Log.i("CurrentActivity", componentName.flattenToShortString());
}
}
}
private ActivityInfo tryGetActivity(ComponentName componentName) {
try {
return getPackageManager().getActivityInfo(componentName, 0);
} catch (PackageManager.NameNotFoundException e) {
return null;
}
}
#Override
public void onInterrupt() {
}
}
You can also list running tasks with the code below:
ActivityManager am = (ActivityManager) getApplicationContext().getSystemService(ACTIVITY_SERVICE);
List li = am.getRunningTasks(100);
Iterator i = li.iterator();
PackageManager pm = getApplicationContext().getPackageManager();
while (i.hasNext()) {
try {
ActivityManager.RunningTaskInfo info = (ActivityManager.RunningTaskInfo)(i.next());
String ac = info.baseActivity.getPackageName();
CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(
ac, PackageManager.GET_META_DATA));
Log.v("asd", c.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
For me the above example did not work. So i ended up using this:
ActivityManager am = (ActivityManager) this
.getSystemService(ACTIVITY_SERVICE);
List<ActivityManager.RecentTaskInfo> l = am.getRecentTasks(1,
ActivityManager.RECENT_WITH_EXCLUDED);
Iterator<ActivityManager.RecentTaskInfo> i = l.iterator();
PackageManager pm = this.getPackageManager();
while (i.hasNext()) {
try {
Intent intent = i.next().baseIntent;
List<ResolveInfo> list = pm.queryIntentActivities(intent,
PackageManager.MATCH_DEFAULT_ONLY);
CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(
list.get(0).activityInfo.packageName,
PackageManager.GET_META_DATA));
Toast.makeText(this, "Application name: " + c.toString(),
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(this,
"Application name not found: " + e.toString(),
Toast.LENGTH_LONG).show();
}
}
With this you can get the current application name
Resources appR = getApplicationContext().getResources();
CharSequence txt = appR.getText(appR.getIdentifier("app_name","string", getApplicationContext().getPackageName()));
System.out.println(txt+" APp Name");
Simply using this code
getActivity().getApplicationInfo().packageName
It gives you application info, and then call the 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.
Try this below, it worked for me.
public static ArrayList<ApplicationInfo> getLaunchableInstalledApps(Context context){
final PackageManager pm = context.getPackageManager();
Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
ArrayList<ApplicationInfo> list = new ArrayList<>();
for (ResolveInfo resolveInfo:pm.queryIntentActivities(intent, PackageManager.GET_META_DATA)){
try {
ApplicationInfo app = context.getPackageManager().getApplicationInfo(resolveInfo.activityInfo.packageName, 0);
list.add(app);
} catch (Exception e) {
e.printStackTrace();
}
}
if (cacheAppsList.isEmpty()){
cacheAppsList.addAll(list);
}
return list;
}
PackageManager pm = getPackageManager();
IntentFilter filter = new IntentFilter();
filter.addAction("android.intent.action.MAIN");
filter.addCategory("android.intent.category.HOME");
filter.addCategory("android.intent.category.DEFAULT");
Context context = getApplicationContext();
ComponentName component = new ComponentName(context.getPackageName(), TestReplaceHomeAppActivity.class.getName());
ComponentName[] components = new ComponentName[] {new ComponentName("com.android.launcher", "com.android.launcher.Launcher"), component};
pm.clearPackagePreferredActivities("com.android.launcher");
pm.addPreferredActivity(filter, IntentFilter.MATCH_CATEGORY_EMPTY, components, component);
I'm having some trouble retrieving icons for installed Android App. The following code returns icons for all apps, however, some apps are returned with small icons (perhaps from drawable - ldpi folder) while some with large icons.
List<PackageInfo> package = getPackageManager().getInstalledPackages(0);
for(PackageInfo p : package){
iApp.icon = p.applicationInfo.loadIcon(getPackageManager());
}
How Can I get icons which are all of the same size, just like in Manage Application section of Android?
I want to add something to previous answer what applies to Android 3.0 and greater.
You can notice that icons retrieved in this standard way (i mean applicationInfo.loadIcon and other typical methods) are smaller than icons you see in launcher app. Scale up just makes icons blurrier. If you want big icons you can use next code (i took it from launcher source code you can find here and changed a bit). Pay your attention to activityManager.getLauncherLargeIconDensity method.
public Drawable getFullResDefaultActivityIcon() {
return getFullResIcon(Resources.getSystem(), android.R.mipmap.sym_def_app_icon);
}
public Drawable getFullResIcon(Resources resources, int iconId) {
Drawable d;
try {
ActivityManager activityManager = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
int iconDpi = activityManager.getLauncherLargeIconDensity();
d = resources.getDrawableForDensity(iconId, iconDpi);
} catch (Resources.NotFoundException e) {
d = null;
}
return (d != null) ? d : getFullResDefaultActivityIcon();
}
public Drawable getFullResIcon(String packageName, int iconId) {
Resources resources;
try {
resources = mContext.getPackageManager().getResourcesForApplication(packageName);
} catch (PackageManager.NameNotFoundException e) {
resources = null;
}
if (resources != null) {
if (iconId != 0) {
return getFullResIcon(resources, iconId);
}
}
return getFullResDefaultActivityIcon();
}
public Drawable getFullResIcon(ResolveInfo info) {
return getFullResIcon(info.activityInfo);
}
public Drawable getFullResIcon(ActivityInfo info) {
Resources resources;
try {
resources = mContext.getPackageManager().getResourcesForApplication(info.applicationInfo);
} catch (PackageManager.NameNotFoundException e) {
resources = null;
}
if (resources != null) {
int iconId = info.getIconResource();
if (iconId != 0) {
return getFullResIcon(resources, iconId);
}
}
return getFullResDefaultActivityIcon();
}
private Drawable getAppIcon(ResolveInfo info) {
return getFullResIcon(info.activityInfo);
}
Hope it helps someone.
Apps might just have different sized icons. The Android Settings app loads icons just like you. It scales the icons when displaying them.
Here's a snippet from packages/apps/Settings/res/layout/manage_applications_item.xml:
<ImageView android:id="#+id/app_icon"
android:layout_width="#android:dimen/app_icon_size"
android:layout_height="#android:dimen/app_icon_size"
android:layout_marginRight="11dip"
android:layout_gravity="center_vertical"
android:scaleType="fitCenter"/>
Although, this has been answered a long time back, here is the complete function. Hope it will help others.
To get the icon, you should query for icon for different dpi.
Try this:
public static Drawable getIconFromPackageName(String packageName, Context context)
{
PackageManager pm = context.getPackageManager();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1)
{
try
{
PackageInfo pi = pm.getPackageInfo(packageName, 0);
Context otherAppCtx = context.createPackageContext(packageName, Context.CONTEXT_IGNORE_SECURITY);
int displayMetrics[] = {DisplayMetrics.DENSITY_XHIGH, DisplayMetrics.DENSITY_HIGH, DisplayMetrics.DENSITY_TV};
for (int displayMetric : displayMetrics)
{
try
{
Drawable d = otherAppCtx.getResources().getDrawableForDensity(pi.applicationInfo.icon, displayMetric);
if (d != null)
{
return d;
}
}
catch (Resources.NotFoundException e)
{
// Log.d(TAG, "NameNotFound for" + packageName + " # density: " + displayMetric);
continue;
}
}
}
catch (Exception e)
{
// Handle Error here
}
}
ApplicationInfo appInfo = null;
try
{
appInfo = pm.getApplicationInfo(packageName, PackageManager.GET_META_DATA);
}
catch (PackageManager.NameNotFoundException e)
{
return null;
}
return appInfo.loadIcon(pm);
}
You must define in App class ;
private Drawable appIcon;
public Drawable getappIcon(){
return this.appIcon;
}
if you use RecyclerViewAdapter ;
List<App> list_app;
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
holder.appIcon.setImageDrawable(list_app.get(position).getappIcon());
}
in MainActivity ;
private List<App> appList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final PackageManager pm = getPackageManager();
List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);
appList = new ArrayList<App>();
for (ApplicationInfo packageInfo : packages) {
Drawable drawableIcon =packageInfo.loadIcon(getPackageManager());
appList.add(new App (drawableIcon));
}