Android-如何以编程方式检查意图过滤器的类型? [英] Android- How to check the type of Intent Filters Programmatically?

查看:110
本文介绍了Android-如何以编程方式检查意图过滤器的类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建具有两个Activity(Activity1和Activity2)的android应用程序,我需要以两种方式打开该应用程序.

I'm Creating an android application with two Activities(Activity1 and Activity2) where I need to open the app in two ways .

方法1:使用NFC卡

这样,我需要打开Activity1.即,如果我刷卡,则需要打开Activity1.

In this way ,I need to open the Activity1. ie., If I swipe the Card I need to open Activity1.

方法2:按图标

这样,我需要打开活动2".即,如果用户单击图标,则必须打开活动2".

In this way,I need to open the Activity 2 .ie., If the user click the icon the Activity2 must be opened .

我的AndroidManifest.xml如下所示,

My AndroidManifest.xml is shown below ,

   ...............
   ...............
   ...............
   <activity 
        android:name=".Activity1"
       android:screenOrientation="portrait"
        android:label="@string/app_name" 
          >
   <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

        <intent-filter>



            <action android:name="android.nfc.action.NDEF_DISCOVERED" />
            <action android:name="android.intent.action.VIEW" />
            <action android:name="android.intent.action.PICK" />
            <category android:name="android.intent.category.DEFAULT" />

            <data android:mimeType="text/plain" />
        </intent-filter>





    </activity>
   .......................
   .......................
   .......................

在Activity1中,我只是尝试通过

In Activity1 I just tried to get the type by

  if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) 


 {
 // Here I'm dealing with Activity1.
 }



 else
 {
 // Here I've set an Intent to go to Activity2.
 }

您可以在else块中设置我要进入Activity2的意图.但是我需要不进入Activity1而直接进入Activity2.

You can look at else block where I've set an Intent to go to Activity2.But I need to go directly to Activity2 without getting into Activity1.

如何实现?请帮助

推荐答案

如果用户单击启动器中的图标,则选择要打开的活动由LAUNCHER类别确定.因此,如果您在清单中定义了第二个活动并将这些行移至该活动,即应该将其从Activity1删除并将其添加到Activity2:

The activity chosen to be opened if the user clicks the icon in the launcher is determined by the LAUNCHER category. So it should work if you define the second activity in the Manifest and move these lines to it, i.e. remove it from Activity1 and add it to Activity2:

<category android:name="android.intent.category.LAUNCHER" />
<action android:name="android.intent.action.MAIN" />

然后,只要用户调用您的应用程序,就会触发Activity2,但其他意图仍然会触发Activity1.

Then whenever the user invokes your application, Activity2 is triggered but Activity1 will still be triggered by the other intents.

参考文献:

这篇关于Android-如何以编程方式检查意图过滤器的类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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