안드로이드 개발 질문/답변
(글 수 45,052)
public class Writer extends Activity {
Spinner wspinner1, wspinner2, wspinner3;
Button writeButton, editButton1, editButton2, editButton3;
CheckBox lockCheck;
String RecordType;
NfcAdapter nfc;
Tag tag = null;
public NdefRecord ndefRecord1, ndefRecord2, ndefRecord3;
public NdefRecord uriRecord, titleRecord, actionRecord;
public NdefMessage ndefMessage = null;
int numRecords = 0;
Boolean isWriting = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.writer);
nfc = NfcAdapter.getDefaultAdapter(this);
ndefRecord1 = ndefRecord2 = ndefRecord3 = null;
uriRecord = titleRecord = actionRecord = null;
Log.d(Common.TAG, "I'm in onCreate()@Writer");
wspinner1 = (Spinner) findViewById(R.id.wspinner1);
wspinner2 = (Spinner) findViewById(R.id.wspinner2);
wspinner3 = (Spinner) findViewById(R.id.wspinner3);
Log.d(Common.TAG, "Spinner done");
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.type_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
wspinner1.setAdapter(adapter);
//wspinner1.setOnItemSelectedListener(new MyOnItemSelectedListener());
wspinner2.setAdapter(adapter);
//wspinner2.setOnItemSelectedListener(new MyOnItemSelectedListener());
wspinner3.setAdapter(adapter);
//wspinner3.setOnItemSelectedListener(new MyOnItemSelectedListener());
Log.d(Common.TAG, "Adapter done");
editButton1 = (Button) findViewById(R.id.button1);
editButton1.setOnClickListener(new EditOnClickListener());
editButton2 = (Button) findViewById(R.id.button2);
editButton2.setOnClickListener(new EditOnClickListener());
editButton3 = (Button) findViewById(R.id.button3);
editButton3.setOnClickListener(new EditOnClickListener());
Log.d(Common.TAG, "Button done");
lockCheck = (CheckBox) findViewById(R.id.lockflag);
writeButton = (Button) findViewById(R.id.button4);
writeButton.setOnClickListener(new OnClickListener() {
//ndefRecord, ndefMessage 변수들이 public 한정자가 없으면 onClick()에서 NullPointerException 오류가 발생한다
public void onClick(View v) {
if (tag == null) {
Toast.makeText(Writer.this, "Tag must be touched to write NDEF Message!", Toast.LENGTH_SHORT).show();
}
else {
byte[] empty = new byte[] {};
if (ndefRecord3 != null) {
Log.d(Common.TAG, "Three Records");
if (ndefRecord1 == null) ndefRecord1 = new NdefRecord(NdefRecord.TNF_UNKNOWN, empty, empty, empty);
if (ndefRecord2 == null) ndefRecord2 = new NdefRecord(NdefRecord.TNF_UNKNOWN, empty, empty, empty);
ndefMessage = new NdefMessage(new NdefRecord[] {ndefRecord1, ndefRecord2, ndefRecord3});
}
else if (ndefRecord2 != null) {
Log.d(Common.TAG, "Two Records");
if (ndefRecord1 == null) ndefRecord1 = new NdefRecord(NdefRecord.TNF_UNKNOWN, empty, empty, empty);
ndefMessage = new NdefMessage(new NdefRecord[] {ndefRecord1, ndefRecord2});
}
else if (ndefRecord1 != null) {
Log.d(Common.TAG, "One Record");
if(ndefRecord1.getType().equals(NdefRecord.TNF_ABSOLUTE_URI)){
ndefMessage = new NdefMessage(new NdefRecord[] {ndefRecord1, NdefRecord.createApplicationRecord("com.qnsolv.nfc.md")});
Log.e("","와나이이이잇" + ndefRecord1.getType());
}else{
Log.e("","와나이123123123123이이잇");
Log.e("","와나이이이122잇" + ndefRecord1.getType());
}
}
else ndefMessage = null;
if (ndefMessage == null) {
Toast.makeText(Writer.this, "At least one record must be filled to write!", Toast.LENGTH_SHORT).show();
}
else {
//Toast.makeText(Writer.this, "Writing started...", Toast.LENGTH_SHORT).show();
new AsyncWriter(Writer.this, ndefMessage, tag).execute();
}
}
}
});
}
public void onActivityResult(int requestCode, int responseCode, Intent i) {
Locale locale = null;
NdefRecord tempRecord = null;
byte[] empty = new byte[] {};
super.onActivityResult(requestCode, responseCode, i);
//Back 버튼을 누르 sub-Activity 클래스에서 RESULT_CANCELED 값을 반환한다
if (responseCode != Activity.RESULT_CANCELED) {
//NdefRecord 필드는 (NFC TNF, RTD TYPE, ID, PAYROAD) 형식으로 구성된다
//RTD TYPE 필드는 각 레코드마다 처리를 달리한다
//ID 필드는 대부분의 경우에 아직 특별한 용도가 없기 때문에 일단 항상 0으로 설정해 둔다
//PAYLOAD 필드는 NFC Forum 표준에 따른 서브 필드들로 구성해서 채워야 한다
switch (requestCode) {
case Common.WK_TEXT:
if (responseCode == Common.RESPONSE_OK) {
//인텐트에서 데이터 추출
String lang = i.getStringExtra("LANG");
String text = i.getStringExtra("TEXT");
Log.d(Common.TAG, lang);
Log.d(Common.TAG, text);
//언어코드 처리
if (lang.equals("ko")) locale = Locale.KOREAN;
else if (lang.equals("en-uk")) locale = Locale.UK;
else if (lang.equals("en-us")) locale = Locale.US;
final byte[] langBytes = locale.getLanguage().getBytes(Charsets.US_ASCII);
//텍스트 처리
final byte[] textBytes = text.getBytes(Charset.forName("UTF_8"));
//페이로드 생성
final int utfBit = 0;
final char status = (char) (utfBit + langBytes.length);
final byte[] data = Bytes.concat(new byte[] {(byte)status}, langBytes, textBytes);
//NFC WELL-KNOWN TYPE 텍스트 레코드 생성
tempRecord = new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_TEXT, new byte[0], data);
}
else if (responseCode == Common.RESPONSE_NOK) {
tempRecord = new NdefRecord(NdefRecord.TNF_UNKNOWN, empty, empty, empty);
}
break;
case Common.WK_URI:
if (responseCode == Common.RESPONSE_OK) {
//인텐트에서 데이터 추출
String uri = i.getStringExtra("URI");
Log.d(Common.TAG, uri);
//WELL-KNOWN TYPE URI 레코드의 PAYLOAD 파트는 한 바이트의 프로토콜 코드(URI 프로토콜 파트)와 나머지 URI 파트로 구성된다
//프로토콜 코드는 메모리 절약을 위해 한 바이트의 코드로 URI PREFIX 부분(예:0x01-->http://www.)을 자동 추가하기 위한 용도로 사용된다
//프로토콜 코드 0x00은 PAYLOAD 부분의 데이터가 프로토콜을 포함한 전체 URI 내용을 가지고 있음을 의미한다
byte[] uriType = new byte[] {(byte)0x00};
byte[] uriBytes = uri.getBytes(Charset.forName("UTF_8"));
byte[] uriPayload = Bytes.concat(uriType, uriBytes);
//NFC WELL-KNOWN TYPE URI 레코드 생성
tempRecord = new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_URI, new byte[0], uriPayload);
}
else if (responseCode == Common.RESPONSE_NOK) {
tempRecord = new NdefRecord(NdefRecord.TNF_UNKNOWN, empty, empty, empty);
}
break;
//SmartPoster 레코드의 PAYLOAD 파트는 NDEF Message 이다(NFC Forum-SmartPoster RTD)
//따라서 단순히 요소 레코드들을 바이트 배열 형태로 붙이기만 하면 SmartPoster 레코드와 별개의 레코드들로 인식을 한다
//요소 레코드들을 이용해서 NDEF 메시지를 먼저 생성하여 바이트 배열로 변환한 후 PAYLOAD 파트에 넣어서 새로운 NDEF 메시지를 만들어야 한다
case Common.WK_SP:
//URI Record 필드는 SmartPoster 레코드의 유일한 필수 항목이기 때문에 이것이 없으면 잘못된 것이다
if (responseCode == Common.RESPONSE_OK && !(i.getStringExtra("URI").equals(null))) {
Log.d(Common.TAG, i.getStringExtra("URI"));
Log.d(Common.TAG, i.getStringExtra("LANG"));
Log.d(Common.TAG, i.getStringExtra("TITLE"));
Log.d(Common.TAG, i.getStringExtra("ACTION"));
int titleFlag = 0, actionFlag = 0;
//인텐트에서 데이터 추출
String uri = i.getStringExtra("URI");
//WELL-KNOWN TYPE URI 레코드
byte[] uriType = new byte[] {(byte)0x00};
byte[] uriBytes = uri.getBytes(Charset.forName("UTF_8"));
byte[] uriPayload = Bytes.concat(uriType, uriBytes);
uriRecord = new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_URI, new byte[0], uriPayload);
String title = i.getStringExtra("TITLE");
if (!(title.equals(null))) {
titleFlag = 1;
Log.d(Common.TAG, "Title Record filled");
String lang = i.getStringExtra("LANG");
//언어코드 처리
if (lang.equals("ko")) locale = Locale.KOREAN;
else if (lang.equals("en-uk")) locale = Locale.UK;
else if (lang.equals("en-us")) locale = Locale.US;
byte[] langBytes = locale.getLanguage().getBytes(Charsets.US_ASCII);
//텍스트 처리
byte[] textBytes = title.getBytes(Charset.forName("UTF_8"));
//페이로드 생성
int utfBit = 0;
char status = (char) (utfBit + langBytes.length);
byte[] titlePayload = Bytes.concat(new byte[] {(byte)status}, langBytes, textBytes);
titleRecord = new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_TEXT, new byte[0], titlePayload);
}
String action = i.getStringExtra("ACTION");
//SmartPoster 레코드의 Action 레코드는 PAYLOAD 길이가 1바이트(Action Code)이고, PAYLOAD TYPE의 Local Type Name 'act' 이다
if (!(action.equals("No Action"))) {
actionFlag = 1;
Log.d(Common.TAG, "Action Record filled");
byte[] localNameType = new byte[] {(byte)0x61, (byte)0x63, (byte)0x74}; //'a' 'c' 't'
byte[] actionPayload = null;
if (action.equals("Do")) {
actionPayload = new byte[] {(byte)0x00}; // Action code: Do
}
else if (action.equals("Save")) {
actionPayload = new byte[] {(byte)0x01}; // Action code: Save
}
else if (action.equals("Edit")) {
actionPayload = new byte[] {(byte)0x02}; // Action code: Edit
}
actionRecord = new NdefRecord(NdefRecord.TNF_WELL_KNOWN, localNameType, new byte[0], actionPayload);
}
//SmartPoster PAYLOAD 구성
byte[] sPPayload = null;
if (titleFlag == 1) {
if (actionFlag == 1) {
NdefMessage tempMessage = new NdefMessage(new NdefRecord[] {uriRecord, titleRecord, actionRecord});
sPPayload = tempMessage.toByteArray();
//sPPayload = Bytes.concat(uriRecord.toByteArray(), titleRecord.toByteArray(), actionRecord.toByteArray());
Log.d(Common.TAG, "uri+title+action");
}
else {
NdefMessage tempMessage = new NdefMessage(new NdefRecord[] {uriRecord, titleRecord});
sPPayload = tempMessage.toByteArray();
//sPPayload = Bytes.concat(uriRecord.toByteArray(), titleRecord.toByteArray());
Log.d(Common.TAG, "uri+title");
}
}
else if (actionFlag == 1) {
NdefMessage tempMessage = new NdefMessage(new NdefRecord[] {uriRecord, actionRecord});
sPPayload = tempMessage.toByteArray();
//sPPayload = Bytes.concat(uriRecord.toByteArray(), actionRecord.toByteArray());
Log.d(Common.TAG, "uri+action");
}
else {
Log.d(Common.TAG, "uri");
NdefMessage tempMessage = new NdefMessage(new NdefRecord[] {uriRecord});
sPPayload = tempMessage.toByteArray();
//sPPayload = uriRecord.toByteArray();
}
//NFC WELL-KNOWN TYPE SmartPoster 레코드 생성
tempRecord = new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_SMART_POSTER, new byte[0], sPPayload);
}
else if (responseCode == Common.RESPONSE_NOK) {
tempRecord = new NdefRecord(NdefRecord.TNF_UNKNOWN, empty, empty, empty);
}
break;
case Common.MIME:
if (responseCode == Common.RESPONSE_OK) {
//인텐트에서 데이터 추출
String mimeType = i.getStringExtra("MIME_TYPE");
String mimeContent = i.getStringExtra("MIME_CONTENT");
Log.d(Common.TAG, mimeType);
Log.d(Common.TAG, mimeContent);
byte[] mimeBytes = mimeType.getBytes(Charset.forName("UTF-8"));
byte[] contentBytes = null;
if (mimeContent.equalsIgnoreCase("Static Handover")) {
contentBytes = i.getByteArrayExtra("BT_OOB_DATA");
Log.d(Common.TAG, Common.byteArrayToHex(contentBytes));
}
else {
contentBytes = mimeContent.getBytes(Charset.forName("UTF-8"));
}
//사설 MIME 타입(application/x-mdapp) 테스트용 샘플 TAG를 생성규칙
//SERVICE-ID=1
// BRANCH-ID=0000: 명동App 구동
// BRANCH-ID=XXXX: 가맹점(XXXX)정보 구동
//SERVICE-ID=2
// MOVIE-ID=XXXXX: 영화(XXXXX)정보 구동
//즉, 컨텐트의 형식은 "SERVICE-ID=1:BRANCH-ID=0000" 이어야 한다
tempRecord = new NdefRecord(NdefRecord.TNF_MIME_MEDIA, mimeBytes, new byte[0], contentBytes);
}
else if (responseCode == Common.RESPONSE_NOK) {
tempRecord = new NdefRecord(NdefRecord.TNF_UNKNOWN, empty, empty, empty);
}
break;
case Common.URL:
if (responseCode == Common.RESPONSE_OK) {
//인텐트에서 데이터 추출
String url = i.getStringExtra("URL");
Log.d(Common.TAG, url);
byte[] urlBytes = url.getBytes(Charset.forName("UTF_8"));
//byte[] urlId = new byte[0];
//Absolute URL 레코드 생성
tempRecord = new NdefRecord(NdefRecord.TNF_ABSOLUTE_URI, urlBytes, new byte[0], urlBytes);
}
else if (responseCode == Common.RESPONSE_NOK) {
tempRecord = new NdefRecord(NdefRecord.TNF_UNKNOWN, empty, empty, empty);
}
break;
case Common.AAR :
if (responseCode == Common.RESPONSE_OK) {
//인텐트에서 데이터 추출
String aar = i.getStringExtra("AAR");
Log.d(Common.TAG, aar);
byte[] urlBytes = aar.getBytes(Charset.forName("UTF_8"));
//byte[] urlId = new byte[0];
//Absolute URL 레코드 생성
tempRecord = new NdefRecord(NdefRecord.TNF_ABSOLUTE_URI, urlBytes, new byte[0], urlBytes);
}
else if (responseCode == Common.RESPONSE_NOK) {
tempRecord = new NdefRecord(NdefRecord.TNF_UNKNOWN, empty, empty, empty);
}
break;
}
switch (i.getIntExtra("R_ID", 0)) {
case 1: ndefRecord1 = tempRecord; break;
case 2: ndefRecord2 = tempRecord; break;
case 3: ndefRecord3 = tempRecord; break;
}
}
}
onActivityResult
여기서 리절트를 다받아주는대
온크리에이트에서도 받는방법이 뭐가있나요 ??
getIntent() -> intentPutExtra 로 받으면 됩니다.