set Android Beam - Android Network

Android examples for Network:NFC

Description

set Android Beam

Demo Code


//package com.java2s;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;

import android.nfc.NfcAdapter;
import android.os.Build;

public class Main {
    @TargetApi(16)//w w w .j  a  v  a2s. co m
    static void setAndroidBeam(Activity activity, String packageName) {
        if (Build.VERSION.SDK_INT < 16)
            return;
        PackageManager pm = activity.getPackageManager();
        NfcAdapter nfcAdapter = getAdapter(activity);
        if (nfcAdapter != null) {
            ApplicationInfo appInfo;
            try {
                appInfo = pm.getApplicationInfo(packageName,
                        PackageManager.GET_META_DATA);
                Uri uris[] = { Uri.parse("file://"
                        + appInfo.publicSourceDir), };
                nfcAdapter.setBeamPushUris(uris, activity);
            } catch (NameNotFoundException e) {
                e.printStackTrace();
            }
        }
    }

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

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

Related Tutorials