Android- How to check the type of Intent Filters P

2019-09-01 08:25发布

问题:

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