在这种情况下,自定义选择器对话框/弹出窗口会更适合您。不要启动意图,而是使用PackageManager 到queryIntentActivities(Intent, int)。从queryIntentActivities(Intent, int) 返回的List<ResolveInfo> 中,使用packageName 过滤掉您自己的应用程序:
String packageName = "";
for(ResolveInfo resInfo : resolvedInfoList) {
packageName = resInfo.activityInfo.applicationInfo.packageName;
// Exclude `packageName` from the dialog/popup that you show
}
编辑 1:
每当调用showList() 时,以下代码将创建并显示PopupWindow。用于返回 popupView 的 xml 布局文件只包含一个 LinearLayout(R.layout.some_popup_view):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/llPopup"
android:orientation="vertical" >
</LinearLayout>
这段代码只是一个简单的演示。为了使它接近可用,您可能需要将带有自定义适配器的ListView 添加到此PopupWindow。在ListView 的OnClickListener 中,您将检索用户单击的应用程序的包名称,并生成启动该活动的意图。截至目前,代码仅显示如何使用自定义选择器过滤掉您自己的应用程序。在 if 块中,将 "com.example.my.package.name" 替换为您的应用程序包名称。
public void showList() {
View popupView = getLayoutInflater().inflate(R.layout.some_popup_view, null);
PopupWindow popupWindow = new PopupWindow(popupView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
LinearLayout llPopup = (LinearLayout) popupView.findViewById(R.id.llPopup);
PackageManager pm = getPackageManager();
Intent intent = new Intent();
// In my case, NfcAdapter.ACTION_NDEF_DISCOVERED was not returning anything
//intent.setAction(NfcAdapter.ACTION_NDEF_DISCOVERED);
intent.setAction(NfcAdapter.ACTION_TECH_DISCOVERED);
List<ResolveInfo> resolvedInfoList = pm.queryIntentActivities(intent, 0);
String packageName = "";
for(ResolveInfo resInfo : resolvedInfoList) {
packageName = resInfo.activityInfo.applicationInfo.packageName;
// Exclude `packageName` from the dialog/popup that you show
if (!packageName.equals("com.example.my.package.name")) {
TextView tv = new TextView(this);
tv.setText(packageName);
llPopup.addView(tv);
}
}
popupWindow.showAtLocation(popupView, Gravity.CENTER, 0, 0);
}