Android Open Source - NFCAndroid Base Activity






From Project

Back to project page NFCAndroid.

License

The source code is released under:

Copyright (c) 2012, Wireless Sensor Technologies, Inc. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following ...

If you think the Android project NFCAndroid listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.gototags.nfc.app;
//  www  . j a v a2s . c  o m
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.IntentFilter;
import android.nfc.NfcAdapter;
import android.os.Bundle;
import android.widget.Toast;

public abstract class BaseActivity extends Activity {
  private NfcAdapter nfcAdapter;
  private PendingIntent pendingIntent;
  private IntentFilter[] intentFilters;
  
  @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       
        this.nfcAdapter = NfcAdapter.getDefaultAdapter(this);
        
        if (this.nfcAdapter != null) {
        IntentFilter intentFilterNdef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
        IntentFilter intentFilterTag = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
        
        this.intentFilters = new IntentFilter[]{ intentFilterNdef, intentFilterTag };
      }
        
        this.pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, this.getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
    }
  
  boolean isNfcEnabled() {
    return this.nfcAdapter != null;
  }
  
  boolean enableForegroundDispatch() {
    if (this.isNfcEnabled()) {
          this.nfcAdapter.enableForegroundDispatch(this, this.pendingIntent, this.intentFilters, null);
          return true;
    } else {
      return false;
    }
  }
  
  boolean disableForegroundDispatch() {
    if (this.isNfcEnabled()) {
      this.nfcAdapter.disableForegroundDispatch(this);
      return true;
    } else {
      return false;
    }
  }
  
  void handleException(Exception exception) {
    if (exception != null)
      this.showToast(exception.toString(), Toast.LENGTH_LONG);
  }
  
  void showToast(String text) {
    this.showToast(text, Toast.LENGTH_SHORT);
  }
  
    void showToast(String text, int duration) {
      if (text != null && !text.isEmpty())
        Toast.makeText(this, text, duration).show();
    }
}




Java Source Code List

com.gototags.nfc.Helper.java
com.gototags.nfc.NfcTag.java
com.gototags.nfc.app.BaseActivity.java
com.gototags.nfc.app.MainActivity.java
com.gototags.nfc.ndef.ParsedNdefRecord.java
com.gototags.nfc.ndef.TextNdefRecord.java
com.gototags.nfc.ndef.UriNdefRecord.java
com.gototags.nfc.records.NdefRecordParser.java
com.gototags.nfc.records.PhoneRecord.java
com.gototags.nfc.records.RecordType.java
com.gototags.nfc.records.Record.java
com.gototags.nfc.records.TextRecord.java
com.gototags.nfc.records.WebsiteRecord.java