【问题标题】:How to pass Android intent to anyone but my own app?如何将 Android 意图传递给我自己的应用程序以外的任何人?
【发布时间】:2013-07-25 18:11:06
【问题描述】:

我有一定的意图 (NDEF_DISCOVERED),其中一些我无法正确处理,所以我想将它们重定向到 android 的默认 nfc 处理程序。

所以我采取了意图,setComponent(null),然后是startActivity(intent)

但是.. 它总是以无限循环的意图抛出返回到我的应用程序。

有没有办法可以向除我的应用程序之外的任何人发送意图?还是发送到android的默认nfc处理程序?

编辑: 所以我使用 vikram 的回答来查询 packagemanager 可能的活动来处理我的意图,然后循环并找到具有最高优先级的活动(不是我)并向他们发送明确的意图。

【问题讨论】:

  • But.. it always comes back to my app in an infinite loop of intent throwing 你到底是什么意思?请显示您创建意图和清单的代码。
  • 你没有回答我的问题。 究竟你说的无限循环是什么意思?您没有显示可能导致无限循环的代码。
  • 我发送了意图(为此我有setComponent(null),所以它没有明确地返回给我。)然后android,因为它自动选择最好的NFC应用程序来处理意图,保持选择我的,我的应用程序会不断接收意图并将其永远发送回去。不是代码中看到的传统无限循环。而是来回传递意图的循环。

标签: java android android-intent nfc


【解决方案1】:

在这种情况下,自定义选择器对话框/弹出窗口会更适合您。不要启动意图,而是使用PackageManagerqueryIntentActivities(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。在ListViewOnClickListener 中,您将检索用户单击的应用程序的包名称,并生成启动该活动的意图。截至目前,代码仅显示如何使用自定义选择器过滤掉您自己的应用程序。在 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);
}

【讨论】:

  • 所以我可以创建选择器而不是 android 这样做?我将如何进行整个过程?如果可以的话,你能告诉我我需要写什么吗?例如,我如何“排除”我的名字?
  • 此外,一旦出现 NFC 选择器弹出窗口,因为我安装了 2 个 nfc 阅读器,但它只显示了这两个。我不知道android的默认nfc阅读器是否会出现在选择器中。
  • @ginsunuva 我现在很忙,无法更新我的答案。当我这样做时,我会发表评论。
  • @ginsunuva 请参见上面的编辑 1
  • 哦,谢谢,哇,我不知道我必须构建一个选择器。但相反,我决定使用 PackageManager 并通读待处理的活动,如果我是唯一的,甚至不开始新的活动。
【解决方案2】:

除了在这里做一个自己的选择器之外,还有另一种选择(不同的选择器可能会让用户感到困惑)

public void rethrowIntentExcludingSelf() {
    final ComponentName component = new ComponentName(this, getClass());
    this.getPackageManager().setComponentEnabledSetting(component, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);

    final Intent intent = this.getIntent();
    intent.setComponent(null);
    this.startActivity(intent);
    new android.os.Handler().postDelayed(
            new Runnable() {
                @Override
                public void run() {
                    getPackageManager().setComponentEnabledSetting(component, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
                }
            },250);
}

我正在使用它——它工作正常——只是不喜欢这个神奇的常数 250——但还没有看到其他方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-17
    • 1970-01-01
    • 2014-06-09
    相关资源
    最近更新 更多