Book Image

Near Field Communication with Android Cookbook

By : Vitor Subtil
Book Image

Near Field Communication with Android Cookbook

By: Vitor Subtil

Overview of this book

Table of Contents (17 chapters)
Near Field Communication with Android Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Filtering URI tags


Filtering activities in the AndroidManifest.xml file can really simplify our work and it allows us to define a more advanced filter than just the content type of a tag. In this recipe, we will learn how we can filter a URI tag.

How to do it...

By performing the following steps, we will create an application that is executed only when the URI parts present in the tags match our filters:

  1. Open Eclipse and create a new Android application project named NfcBookCh2Example5 with the package name nfcbook.ch2.example5.

  2. Open MainActivity, override the onNewIntent method, and insert the following code:

    @Override
    protected void onNewIntent(Intent intent) {
    
      if ( isNfcIntent(intent) ) {
            
        Toast.makeText( this, "NFC intent received", Toast.LENGTH_SHORT).show();
        
      }
      super.onNewIntent(intent);
    }
  3. Implement the isNfcIntent method:

    boolean isNfcIntent(Intent intent) {
    return intent.hasExtra(NfcAdapter.EXTRA_TAG);
    }
  4. Open the AndroidManifest.xml file and add the following intent...