Android-如何获取动态意图的软件包详细信息 [英] Android - How do I get package details for dynamic intents
问题描述
因此,我正在制作自定义应用选择器.它将安装浏览器,并安装所有地图应用程序.我得到的每一个如下:
So I am making a custom app chooser. It will have the browsers installed, and any map applications installed. I get each of these as follows:
PackageManager packageManager = activity.getPackageManager();
//This gets all the browsers
String browserURI = "http://"+Driver.getIamhereURL()+lat+","+lon;
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(browserURI));
List<ResolveInfo> browserList = packageManager.queryIntentActivities(browserIntent, 0);
// This gets all the Map apps:
String mapUri = "geo:"+lat+","+lon;
Intent mapIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(mapUri));
List<ResolveInfo> mapList = packageManager.queryIntentActivities(mapIntent, 0);
//Combine the two
browserList.addAll(mapList);
//Now build layout and return a LinearLayout
LinearLayout appListBody = buildAppList(browserList,packageManager);
因此 appListBody
将希望包含应用程序图标和应用程序名称.显然,每个应用程序都必须具有与之关联的 onClickListener()
,这将启动 intent
.我的问题是,当我只能得到 List< ResolveInfo>listOfApps
?
So appListBody
will containt the app icon and app name hopefully. Obviously each app will have to have an onClickListener()
associated with it, which will launch the intent
. My problem is, how do I send the intent
to my method, when all I can get is List<ResolveInfo> listOfApps
?
我可以尝试使用 for
循环并进入 pm.getLaunchIntentForPackage(listOfApps.get(count).resolvePackageName)
,但是随后我得到 NullPointerException:包名称为空
.
I can try a for
loop and go pm.getLaunchIntentForPackage(listOfApps.get(count).resolvePackageName)
but then I get NullPointerException: package name is null
.
任何人都可以帮忙吗?
谢谢.
推荐答案
我的问题是,当我只能得到List listOfApps时,如何将意图发送给我的方法?
My problem is, how do I send the intent to my method, when all I can get is List listOfApps?
您可以自己构建合适的 Intent
:
You can construct a suitable Intent
yourself:
@Override
protected void onListItemClick(ListView l, View v,
int position, long id) {
ResolveInfo launchable=adapter.getItem(position);
ActivityInfo activity=launchable.activityInfo;
ComponentName name=new ComponentName(activity.applicationInfo.packageName,
activity.name);
Intent i=new Intent(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_LAUNCHER);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
i.setComponent(name);
startActivity(i);
}
这来自我的 Launchalot 示例项目装有 ResolveInfo
对象的适配器上的 ListView
.
This is from my Launchalot sample project, which has a ListView
on an adapter full of ResolveInfo
objects.
我认为您的解决方案如果不是更好的话,将是等效的,但是您将需要使用 activity.applicationInfo.packageName
来使用该软件包. resolvePackageName
不是您认为的那样.:-)
I would think that your solution would be equivalent if not superior, but you will need to use activity.applicationInfo.packageName
to get the package to use. resolvePackageName
is not what you think it is. :-)
这篇关于Android-如何获取动态意图的软件包详细信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!