在这种情况下,自定义选择器对话框/弹出窗口对您来说会更好。不要启动意图,而是使用PackageManagerto queryIntentActivities(Intent, int)。从返回的List<ResolveInfo>内容中queryIntentActivities(Intent, int),使用以下内容过滤掉您自己的应用程序packageName:
String packageName = "";
for(ResolveInfo resInfo : resolvedInfoList) {
packageName = resInfo.activityInfo.applicationInfo.packageName;
// Exclude `packageName` from the dialog/popup that you show
}
编辑 1:
以下代码将创建并显示一个PopupWindow无论何时showList()被调用。用于返回的 xml 布局文件只popupView包含一个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带有自定义适配器的 this PopupWindow。在OnClickListenerfor 中ListView,您将检索用户单击的应用程序的包名称,并生成启动该活动的意图。截至目前,代码仅显示如何使用自定义选择器过滤掉您自己的应用程序。在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);
}