set NFC Push Message - Android Network

Android examples for Network:NFC Message

Description

set NFC Push Message

Demo Code


//package com.java2s;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;

import android.net.Uri;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.NfcAdapter;
import android.os.Build;

public class Main {
    @TargetApi(14)/*from  w  w  w . jav  a2s . co m*/
    public static boolean setPushMessage(Activity activity, Uri toShare) {
        NfcAdapter adapter = getAdapter(activity);
        if (adapter != null) {
            adapter.setNdefPushMessage(new NdefMessage(
                    new NdefRecord[] { NdefRecord.createUri(toShare), }),
                    activity);
            return true;
        }
        return false;
    }

    @TargetApi(14)
    private static NfcAdapter getAdapter(Context context) {
        if (Build.VERSION.SDK_INT < 14)
            return null;

        return NfcAdapter
                .getDefaultAdapter(context.getApplicationContext());
    }
}

Related Tutorials