Last active
December 13, 2015 17:58
-
-
Save esperia/4951260 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE HTML> | |
<html lang="ja"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>NfcRealLike - 第3回関西NFCLab勉強会</title> | |
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"> | |
<style type="text/css"> | |
body { | |
text-align: center; | |
} | |
h3 { | |
padding-bottom: 10px; | |
border-bottom: 1px solid #EEE; | |
} | |
</style> | |
</head> | |
<body> | |
<header> | |
<h1>NFCLab</h1> | |
<h3>NFCLabにいいね!しよう!</h3> | |
</header> | |
<div class="container"> | |
<iframe src="//www.facebook.com/plugins/like.php?href=http%3A%2F%2Ffacebook.com%2FNFCLab&send=false&layout=box_count&width=70&show_faces=false&font&colorscheme=light&action=like&height=90&appId=456232687764395" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:70px; height:90px;" allowTransparency="true"></iframe> | |
</div> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.nfc_lab.simple_nfcreallike; | |
import java.io.IOException; | |
import java.util.Arrays; | |
import android.annotation.TargetApi; | |
import android.app.Activity; | |
import android.app.PendingIntent; | |
import android.content.Intent; | |
import android.net.Uri; | |
import android.nfc.FormatException; | |
import android.nfc.NdefMessage; | |
import android.nfc.NdefRecord; | |
import android.nfc.NfcAdapter; | |
import android.nfc.Tag; | |
import android.nfc.TagLostException; | |
import android.nfc.tech.Ndef; | |
import android.nfc.tech.NdefFormatable; | |
import android.os.Build; | |
import android.os.Bundle; | |
import android.view.Menu; | |
import android.widget.Toast; | |
public class MainActivity extends Activity { | |
private NfcAdapter mNfcAdapter; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
// NFC Adapter を取得する。 | |
mNfcAdapter = NfcAdapter.getDefaultAdapter(this); | |
} | |
@Override | |
protected void onResume() { | |
super.onResume(); | |
Intent intent = new Intent(this, this.getClass()) | |
.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | |
| Intent.FLAG_ACTIVITY_NEW_TASK); | |
PendingIntent pIntent = PendingIntent.getActivity( | |
getApplicationContext(), 0, intent, 0); | |
mNfcAdapter.enableForegroundDispatch(this, pIntent, null, null); | |
} | |
@Override | |
protected void onPause() { | |
super.onPause(); | |
mNfcAdapter.disableForegroundDispatch(this); | |
} | |
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) | |
@Override | |
protected void onNewIntent(Intent intent) { | |
super.onNewIntent(intent); | |
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); | |
if (tag == null) { | |
Toast.makeText(this, "タグが読み取れませんでした。もう一度タグかざしてください。", | |
Toast.LENGTH_SHORT).show(); | |
return; | |
} | |
writeToCard(tag, NdefRecord.createUri(Uri | |
.parse("http://nfc-reallike.appspot.com/like"))); | |
} | |
public void writeToCard(Tag tag, NdefRecord... params) { | |
String message; | |
try { | |
if (Arrays.asList(tag.getTechList()).contains( | |
NdefFormatable.class.getName())) { | |
writeNdefFormatable(tag, params); | |
} else if (Arrays.asList(tag.getTechList()).contains( | |
Ndef.class.getName())) { | |
writeNdef(tag, params); | |
} else { | |
throw new RuntimeException("対応していないタグです。"); | |
} | |
message = "URLを書き込みました!"; | |
} catch (Exception e) { | |
message = "書き込めませんでした。。"; | |
} | |
Toast.makeText(this, message, Toast.LENGTH_LONG).show(); | |
} | |
private void writeNdefFormatable(Tag tag, NdefRecord[] params) | |
throws TagLostException, FormatException, IOException { | |
NdefFormatable ndef = NdefFormatable.get(tag); | |
try { | |
ndef.connect(); | |
ndef.format(new NdefMessage(params)); | |
} finally { | |
ndef.close(); | |
} | |
} | |
private void writeNdef(Tag tag, NdefRecord[] params) | |
throws TagLostException, FormatException, IOException { | |
Ndef ndef = Ndef.get(tag); | |
try { | |
ndef.connect(); | |
ndef.writeNdefMessage(new NdefMessage(params)); | |
} finally { | |
ndef.close(); | |
} | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
// Inflate the menu; this adds items to the action bar if it is present. | |
getMenuInflater().inflate(R.menu.activity_main, menu); | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment