java 如何获取其他应用程序的图标(Android)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2384713/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the icon of other applications (Android)
提问by Joe
What I'm doing is getting a list of all the current running processes on the phone. Which I have done by,
我正在做的是获取手机上所有当前正在运行的进程的列表。我所做的,
private List<RunningAppProcessInfo> process;
private ActivityManager activityMan;
...
activityMan = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
process = activityMan.getRunningAppProcesses();
this works fine. When I call the processName field like
这工作正常。当我调用 processName 字段时
process.get(i).processName;
I get a name like com.android.mail for example.
例如,我得到一个像 com.android.mail 这样的名字。
what I'm trying to do is use this to get access to that application so I can display its icon to the user, but I cant find anything that lets me do this. Is there something that can help me?
我想要做的是使用它来访问该应用程序,以便我可以向用户显示它的图标,但我找不到任何可以让我这样做的东西。有什么可以帮助我的吗?
I'm testing this app on my hero so the api level is 3 (android 1.5).
我正在我的英雄上测试这个应用程序,所以 api 级别是 3 (android 1.5)。
Thanks.
谢谢。
回答by Joe
Ok, I figured out how to do it. In case your curious this is what I did.
好的,我想出了怎么做。如果你好奇,这就是我所做的。
private PackageManager pk;
pk = getPackageManager();
....
pk.getApplicationIcon(process.get(i).processName)
Thanks.
谢谢。
回答by j2emanue
try this way make a class called packageinformation:
尝试这种方式创建一个名为 packageinformation 的类:
public class PackageInformation{
公共类包信息{
private Context mContext;
public PackageInformation(Context context){
mContext=context;
}
class InfoObject {
public String appname = "";
public String pname = "";
public String versionName = "";
public int versionCode = 0;
public Drawable icon;
public void InfoObjectAggregatePrint() {//not used yet
Log.v(appname,appname + "\t" + pname + "\t" + versionName + "\t" + versionCode);
}
} private ArrayList getPackages() { ArrayList apps = getInstalledApps(false); /* false = no system packages */ final int max = apps.size(); for (int i=0; i
} private ArrayList getPackages() { ArrayList apps = getInstalledApps(false); /* false = 没有系统包 */ final int max = apps.size(); 对于 (int i=0; i
public ArrayList<InfoObject> getInstalledApps(boolean getSysPackages) {
ArrayList<InfoObject> res = new ArrayList<InfoObject>();
List<PackageInfo> packs = mContext.getPackageManager().getInstalledPackages(0);
for(int i=0;i<packs.size();i++) {
PackageInfo p = packs.get(i);
if ((!getSysPackages) && (p.versionName == null)) {
continue ;
}
InfoObject newInfo = new InfoObject();
newInfo.appname = p.applicationInfo.loadLabel(mContext.getPackageManager()).toString();
newInfo.pname = p.packageName;
newInfo.versionName = p.versionName;
newInfo.versionCode = p.versionCode;
newInfo.icon = p.applicationInfo.loadIcon(mContext.getPackageManager());
res.add(newInfo);
}
return res;
}
}
tuck this away somewhere and now to access the info from your working class do this:
把它藏在某个地方,现在从你的工人阶级那里访问信息,这样做:
PackageInformation androidPackagesInfo=new PackageInformation(this);
ArrayList<InfoObject> appsData=androidPackagesInfo.getInstalledApps(true);
for (InfoObject info : appsData) {
Toast.makeText(MainActivity.this, info.appname,2).show();
Drawable somedrawable=info.icon;
}
}
回答by Andrei Buneyeu
Since Android 3.0 you might want to get a bigger launcher icon that you can't get the way you described. If so, perhaps my answer to question below can help you: Getting App Icon in Android
从 Android 3.0 开始,您可能希望获得更大的启动器图标,而您无法按照您的描述方式获得。如果是这样,也许我对以下问题的回答可以帮助您:在 Android 中获取应用程序图标
回答by Vinay
While packagemanager.getApplicationIcon(pkagename) works for some apps but not for all.
虽然 packagemanager.getApplicationIcon(pkagename) 适用于某些应用程序,但不适用于所有应用程序。
Found better answer here: how to get running applications icon on android programmatically
在这里找到更好的答案: how to get running applications icon on android programmatically
Uses:
用途:
Drawable ico=getApplicationInfo(packagemanager,PackageManager.GET_META_DATA).loadIcon(getPackageManager());

