Skip to content

Instantly share code, notes, and snippets.

@andersonleite
Created June 30, 2015 13:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andersonleite/2215cac73bf6910414ba to your computer and use it in GitHub Desktop.
Save andersonleite/2215cac73bf6910414ba to your computer and use it in GitHub Desktop.
package andersonleite.com.pretest;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.IntentFilter;
import android.nfc.NfcAdapter;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
public class MainActivity extends ActionBarActivity {
private static final String TAG = "NFC";
private NfcAdapter nfcAdapter;
private PendingIntent nfcPendingIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// initialize NFC
nfcAdapter = NfcAdapter.getDefaultAdapter(this);
nfcPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, this.getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
}
@Override
protected void onResume() {
super.onResume();
Log.d(TAG, "enableForegroundMode");
// foreground mode gives the current active application priority for reading scanned tags
IntentFilter tagDetected = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED); // filter for tags
IntentFilter[] writeTagFilters = new IntentFilter[] {tagDetected};
nfcAdapter.enableForegroundDispatch(this, nfcPendingIntent, writeTagFilters, null);
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
Log.d(TAG, "onNewIntent");
// check for NFC related actions
if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) {
ImageView lata = (ImageView) findViewById(R.id.lata);
if(lata.getVisibility()== View.VISIBLE)
lata.setVisibility(View.INVISIBLE);
else{
lata.setVisibility(View.VISIBLE);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment