如何通过Android的意图任何人,但我自己的应用程序? [英] How to pass Android intent to anyone but my own app?

查看:125
本文介绍了如何通过Android的意图任何人,但我自己的应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一定的意图( NDEF_DISCOVERED ),其中一些我不能正确处理,所以我想重定向这些Android的默认NFC处理程序。

I have a certain intent (NDEF_DISCOVERED), some of which I cannot handle correctly, so I want to redirect those to android's default nfc handler.

所以,我走的意图, setComponent(空),然后 startActivity(意向)

So i take the intent, setComponent(null), and then startActivity(intent)

但..它总是回来我的应用程序中意图抛出一个无限循环。

But.. it always comes back to my app in an infinite loop of intent throwing.

有没有一种方法可以让我送行的意图,任何人,但我的应用程序?或者发送到Android的默认NFC处理?

Is there a way I can send off an intent to anyone but my app? Or send it to android's default nfc handler?

编辑: 所以我用维克拉姆的答案查询packagemanager可能的活动来处理我的意图,那么环通,发现具有最高优先级的活动(谁是不是我),并发出了明确的意图给他们。

So I used vikram's answer to query the packagemanager for possible activities to handle my intent, then looped thru and found the activity with the highest priority (who isn't me) and sent an explicit intent to them.

推荐答案

自定义选择对话框/弹出将在这种情况下,更好地为您服务。相反,启动一个意图,使用 PackageManager queryIntentActivities(意图,INT)。从名单,其中,ResolveInfo> queryIntentActivities(意图,INT)返回,用筛选出自己的应用程序的的packageName

A custom chooser dialog/popup will be better for you in this case. Instead of launching an intent, use the PackageManager to queryIntentActivities(Intent, int). From the List<ResolveInfo> that queryIntentActivities(Intent, int) returns, filter out your own app using the packageName:

String packageName = "";
for(ResolveInfo resInfo : resolvedInfoList) {

    packageName = resInfo.activityInfo.applicationInfo.packageName;

    // Exclude `packageName` from the dialog/popup that you show

}

修改1

下面code将创建并显示一个 PopupWindow 每当 showList()被调用。用于返回XML布局文件 popupView 包含只是一个的LinearLayout (R.layout.some_popup_view):

The following code will create and show a PopupWindow whenever showList() is called. The xml layout file used to return popupView contains nothing but a 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>

这code只是一个简单的演示。因为这是任何接近有用的,你可能需要添加一个的ListView 与自定义适配器本 PopupWindow 。在 OnClickListener 的ListView ,你将检索用户点击应用程序的包名,并生成意图启动该活动。截至目前,在code只显示了如何使用自定义选择器来过滤掉自己的应用程序。在如果块,替换com.example.my.package.name与您的应用程序包名称。

This code is just a simple demonstration. For it to be anything close to usable, you will probably need to add a ListView with a custom adapter to this PopupWindow. In the OnClickListener for the ListView, you will retrieve the package name of the Application that the user clicks on, and generate an intent to start that activity. As of now, the code only displays how to filter out your own application using a custom chooser. In the if block, replace "com.example.my.package.name" with your app's 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的意图任何人,但我自己的应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆