'간단'에 해당되는 글 2

  1. 2014.09.26 NFC 태그 쓰기(Write)
  2. 2014.01.14 SharedPreferences 사용법을 알아보자. 1
Android/NFC | Posted by 덩치 2014. 9. 26. 17:02

NFC 태그 쓰기(Write)

펌 OK (출처 표시), 상업적 이용 NO, 컨텐츠 변경 NO

이전 포스팅 NFC 태그 읽기 에 이어서 쓰기에 대해 알아보자


NFCWrite하기 위해서는 NDEF Message를 구성해야한다. (참고 : http://biig.tistory.com/77)


참고 글을 봤다면 NDEF Message가 어떻게 구성되는지는 다 알것이고 설명은 생략하겠다.



우선, NFC 관련 객체를 생성한다.


public static final int TYPE_TEXT = 1;

public static final int TYPE_URI = 2;


EditText mWriteText;

NfcAdapter mNfcAdapter;

PendingIntent mPendingIntent;


protected void onCreate(Bundle saveInstanceState) {


mWriteText = (EditText) findViewById(R.id.et_write);

mNfcAdapter = NfcAdapter.getDefaultAdapter(this);

Intent inent = new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

mPendingIntent = PendingIntent.getActivity(this,0,intent,0);


}


다음, 이전 글에서 설명했듯이 태그를 인식하게되면 onNewIntent 메소드가 호출된다.


onNewIntent가 호출되면 태그에 실제로 값을 Write하는 작업을 수행해야한다.


@Override

protected void onNewIntent(Intent intent) {

super.onNewIntent(intent);

if (intent == null)

return;


Tag detectedTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);

writeTag(getTextAsNdef(), detectedTag);


}


private NdefMessage getTextAsNdef() { 


byte[] textBytes = mWriteText.getText().toString().getBytes();


NdefRecord textRecord = new NdefRecord(NdefRecord.TNF_MIME_MEDIA, 

"text/plain".getBytes(),

new byte[] {}, 

textBytes);

return new NdefMessage(new NdefRecord[] {textRecord});

}


private NdefMessage getUriAsNdef() { 


byte[] textBytes = mWriteText.getText().toString().getBytes();


NdefRecord record1 = new NdefRecord(NdefRecord.TNF_WELL_KNOWN,

new String("U").getBytes(Charset.forName("US-ASCII")),

new byte[0],

textBytes) ;

return new NdefMessage(new NdefRecord[] {textRecord});

}


private void toast(String text) {

Log.i("fureun","toast");

Toast.makeText(this, text, Toast.LENGTH_SHORT).show();

}


이렇게 하면 기본적인 Write가 완료가 된다. 이후 Read를 하게되면 자신이 입력한 태그의 정보를 확인할 수 있다.



'Android > NFC' 카테고리의 다른 글

NFC 태그 읽기(Read)  (0) 2014.09.26
NFC란 ?  (0) 2014.09.25

펌 OK (출처 표시), 상업적 이용 NO, 컨텐츠 변경 NO

안드로이드에서 데이터 관리를 할 수 있는 방법에는 여러가지가 있다.

SQLite DB를 이용한 데이터 관리, 서버를 거친 DB에 의한 관리, Input,OutputStreamReader를 이용한 관리,

SharedPreferences를 이용한 관리 등이 있으며, 여기서는 SharedPreferences를 이용한 방법에 대해

알아보겠다.


SharedPreferences키와 벨류로 이루어진 파일 형식으로 데이터를 저장하는 방법으로,

Boolean, Float, Int, Long, String 형태로 데이터를 저장할 수 있다.

어플의 설정값, 사용자 설정 정보등을 저장하는데 적합하며 사용법은 다음과 같다.


선언

SharedPreferences mPref = PreferenceManager.getDefaultSharedPreferences(context);


데이터 저장

SharedPreferences.Editor editor = mPref.edit();

editor.putString("key", "value");

editor.commit();


데이터 호출

String callValue = mPref.getString("key", "default value");

*호출하는 키에 대응하는 값이 없을 경우 디폴트로 설정한 데이터가 출력된다.


데이터 삭제

SharedPreferences.Editor editor = mPref.edit();

editor.remove("key"); or editor.clear();

editor.commit();



주의할점은 만약 기존 데이터의 형을 변경시키고싶다면

반드시 클리어를 한 다음 변경사항을 적용시킨다. 그렇지않으면 예외가 발생하게된다.


SharedPreferences의 장점은 심플한 사용과 관리가 아닐까 한다.



'Android > 입/출력' 카테고리의 다른 글

기본적인 JSONObject, JSONArray 사용법  (3) 2014.03.21