gin*_*uva 5 java android nfc android-intent
我有一个intent(NDEF_DISCOVERED),其中一些我无法正确处理,所以我想将它们重定向到android的默认nfc处理程序.
所以我采取了意图,setComponent(null)然后startActivity(intent)
但是......它总是在无限循环的意图投掷中回到我的应用程序.
除了我的应用程序,我有没有办法向任何人发送意图?或者将它发送到android的默认nfc处理程序?
编辑:所以我使用vikram的答案来查询packagemanager以寻找可能的活动来处理我的意图,然后循环并发现具有最高优先级的活动(谁不是我)并向他们发送了明确的意图.
在这种情况下,自定义选择器对话框/弹出窗口对您来说会更好.而不是启动意图,使用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
}
Run Code Online (Sandbox Code Playgroud)
编辑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>
Run Code Online (Sandbox Code Playgroud)
这段代码只是一个简单的演示.为了使它可以接近可用,您可能需要添加ListView一个自定义适配器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);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1075 次 |
| 最近记录: |