Skip to content

Instantly share code, notes, and snippets.

@meco300
Created January 18, 2013 03:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save meco300/4562035 to your computer and use it in GitHub Desktop.
Save meco300/4562035 to your computer and use it in GitHub Desktop.
後は適当に、おみくじっぽいものが引けるアプリにしてみました。ResultActivity画面を作成。NFCのIDと当日の日付を足して、割り切れた位置で運勢を決めます。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f0e68c"
tools:context=".ResultActivity" >
<TextView
android:id="@+id/textview01_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world" />
<ImageView
android:id="@+id/imageview01_id"
android:layout_height="fill_parent"
android:layout_width="fill_parent" />
</RelativeLayout>
package com.test.nfc_test;
import java.io.UnsupportedEncodingException;
import android.net.Uri;
import android.nfc.*;
import android.os.Bundle;
import android.os.Parcelable;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.PendingIntent;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.text.Editable;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
public static final String LOG_TAG = MainActivity.class.getSimpleName();
private NfcAdapter mNfcAdapter;
EditText mNote;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@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;
}
@Override
protected void onResume(){
super.onResume();
mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
//▼NFCの機能判定
//NFC機能なし機種
if(mNfcAdapter == null){
Toast.makeText(getApplicationContext(), "no Nfc feature", Toast.LENGTH_SHORT).show();
finish();
return;
}
//NFC通信OFFモード
if(!mNfcAdapter.isEnabled()){
Toast.makeText(getApplicationContext(), "off Nfc feature", Toast.LENGTH_SHORT).show();
finish();
return;
}
//▲NFCの機能判定
//NFCを見つけたときに反応させる
//PendingIntent→タイミング(イベント発生)を指定してIntentを発生させる
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this,getClass()), 0);
//タイミングは、タグ発見時とする。
IntentFilter[] intentFilter = new IntentFilter[]{
new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED)
};
//反応するタグの種類を指定。
String[][] techList = new String[][]{
{
android.nfc.tech.NfcA.class.getName(),
android.nfc.tech.NfcB.class.getName(),
android.nfc.tech.IsoDep.class.getName(),
android.nfc.tech.MifareClassic.class.getName(),
android.nfc.tech.MifareUltralight.class.getName(),
android.nfc.tech.NdefFormatable.class.getName(),
android.nfc.tech.NfcV.class.getName(),
android.nfc.tech.NfcF.class.getName(),
}
};
mNfcAdapter.enableForegroundDispatch(this, pendingIntent, intentFilter, techList);
}
@Override
public void onPause(){
super.onPause();
//アプリが表示されてない時は、NFCに反応しなくてもいいようにする
mNfcAdapter.disableForegroundDispatch(this);
}
//NFCをタッチした後の処理
@Override
protected void onNewIntent(Intent intent){
super.onNewIntent(intent);
String action = intent.getAction();
if(TextUtils.isEmpty(action)){
return;
}
if(!action.equals(NfcAdapter.ACTION_TAG_DISCOVERED)){
return;
}
//NFCのIDを取得。byte配列。
byte[] rawId = intent.getByteArrayExtra(NfcAdapter.EXTRA_ID);
String id = "nothing";
//Stringに変換して表示させてみる
id = bytesToString(rawId);
Toast.makeText(getApplicationContext(), id, Toast.LENGTH_SHORT).show();
//▼NFCの中身を取得する
//NdefMessageをParcelable型で取得。NdefMessageが並列でいくつかあるパターンがある。
Parcelable[] rawMessage = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
if(rawMessage != null){
//Parcelable型からNdefMessage型に入れ直す。
NdefMessage[] msgs = new NdefMessage[rawMessage.length];
String str = "";
for(int i=0; i<rawMessage.length; i++){
msgs[i] = (NdefMessage)rawMessage[i];
//NdefMessageをNdefRecordにバラす。
for(NdefRecord record : msgs[i].getRecords()){
//データ本体のPayload部を取り出す。バイト配列。
byte[] payload = record.getPayload();
for(byte data : payload){
//負の値が入ってる場合があるので"& 0xff"をつける
str += String.format("%c", data & 0xff);
}
}
Toast.makeText(getApplicationContext(), str, Toast.LENGTH_SHORT).show();
}
}
else{
Toast.makeText(getApplicationContext(), "中身のないタグです", Toast.LENGTH_SHORT).show();
}
//データ持って画面遷移
int[] intList = byteToInt(rawId);
Intent intent2 = new Intent(MainActivity.this, ResultActivity.class);
intent2.putExtra("idIntList", intList);
startActivity(intent2);
}
//byte配列をStringにして返す
public String bytesToString(byte[] bytes){
StringBuilder buffer = new StringBuilder();
boolean isFirst = true;
for(byte b : bytes){
if(isFirst){
isFirst = false;
} else {
buffer.append("-");
}
buffer.append(Integer.toString(b & 0xff));
}
return buffer.toString();
}
//byte配列をInt配列にして返す
public int[] byteToInt(byte[] bytes){
int[] result = new int[bytes.length];
for(int i = 0; i < bytes.length; i++){
result[i] = bytes[i] & 0xff;
}
return result;
}
}
package com.test.nfc_test;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class ResultActivity extends Activity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
//画面に表示するコンポーネント
TextView text = (TextView)findViewById(R.id.textview01_id);
ImageView image = (ImageView)findViewById(R.id.imageview01_id);
Intent intent = getIntent();
//NFCのIdと日付をこねこねして乱数ぽいのをつくる
int[] idIntList = intent.getIntArrayExtra("idIntList");
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
int fortuneNum = Integer.parseInt(sdf.format(date));
for(int id : idIntList){
fortuneNum += id;
}
//割り切れたとこが今日の運勢
String fortune = "運勢";
if(fortuneNum % 5 == 0 ){
fortune = "大吉";
image.setImageResource(R.drawable.daikichi);
}else if(fortuneNum % 4 == 0 ){
fortune = "小吉";
image.setImageResource(R.drawable.daikichi);
}else if(fortuneNum % 3 == 0 ){
fortune = "末吉";
image.setImageResource(R.drawable.daikichi);
}else if(fortuneNum % 2 == 0 ){
fortune = "中吉";
image.setImageResource(R.drawable.daikichi);
}else if(fortuneNum % 6 == 0 ){
fortune = "凶";
image.setImageResource(R.drawable.daikichi);
}else{
fortune = "未知数";
image.setImageResource(R.drawable.daikichi);
}
Toast.makeText(getApplicationContext(), "今日の運勢は" + fortune + "です!", Toast.LENGTH_SHORT).show();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment