在这种情况下,自定义选择器对话框/弹出窗口将更适合您。不要启动意图,而是使用PackageManager
to queryIntentActivities(Intent, int)
。来自List<ResolveInfo>
that queryIntentActivities(Intent, int)
返回,使用过滤掉您自己的应用程序packageName
:
String packageName = "";
for(ResolveInfo resInfo : resolvedInfoList) {
packageName = resInfo.activityInfo.applicationInfo.packageName;
// Exclude `packageName` from the dialog/popup that you show
}
Edit 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
带有一个自定义适配器PopupWindow
。在里面OnClickListener
为了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);
}