Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 * Copyright 2013 Gerhard Klostermeier
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import android.app.Activity;
import android.app.PendingIntent;

import android.content.Intent;
import android.nfc.NfcAdapter;

import android.nfc.tech.NfcA;

public class Main {
    private static NfcAdapter mNfcAdapter;

    /**
     * Enables the NFC foreground dispatch system for the given Activity.
     * 
     * @param targetActivity The Activity that is in foreground and wants to have NFC Intents.
     * @see #disableNfcForegroundDispatch(android.app.Activity)
     */
    public static void enableNfcForegroundDispatch(Activity targetActivity) {
        if (mNfcAdapter != null && mNfcAdapter.isEnabled()) {

            Intent intent = new Intent(targetActivity, targetActivity.getClass())
                    .addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
            PendingIntent pendingIntent = PendingIntent.getActivity(targetActivity, 0, intent, 0);
            mNfcAdapter.enableForegroundDispatch(targetActivity, pendingIntent, null,
                    new String[][] { new String[] { NfcA.class.getName() } });
        }
    }
}