Android- How to check the type of Intent Filters Programmatically?

∥☆過路亽.° 提交于 2019-12-24 22:51:13

问题


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

Way 1 : By NFC Card

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

Way 2 : By Icon

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

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>
   .......................
   .......................
   .......................

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.
 }

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.

How to achieve that ? Please help


回答1:


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" />

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

References:

  • Intents and Filters


来源:https://stackoverflow.com/questions/17469815/android-how-to-check-the-type-of-intent-filters-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!