gpt4 book ai didi

android - 如何以编程方式在android上获取正在运行的应用程序图标

转载 作者:太空宇宙 更新时间:2023-11-03 11:40:09 26 4
gpt4 key购买 nike

下面是我的代码,但我正在为所有正在运行的应用程序获取默认的 android 启动器图标:

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

最佳答案

只需替换这一行

ico = pm.getApplicationIcon(pName);

对此

ico = getApplicationInfo().loadIcon(getPackageManager()); 

已编辑完整代码:

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

}
}
}

上面的代码是向你显示像这样的图标:

enter image description here

关于android - 如何以编程方式在android上获取正在运行的应用程序图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15567957/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com