NFC 태그를 이용한 매장용 대기순번 알림 서비스 어플리케이션을 만들려고 하는데
제가 막히는 부분에 대해서 설명을 드리자면 소유한 단말기로 NFC를 태깅 시 순번 1이라는 데이터를 받아 옵니다.
그리고 나서 다른 단말기로 NFC를 태깅하면 대기순번 카운터 값이 증가되면서 2라는 대기순번을 받아 오려고 하는데(또 다시 다른 단말기로 태깅하면 3이라는 번호를 받아오겠죠)
인터넷에 찾아보니 SharedPreferences 라는 클래스가 DB와 같은 역할을 한다고 나와있더라구요
일단 SharedPreferences 라는 클래스를 이용하여 단말기가 순번 1이라는 데이터를 받아오면 그 즉시 카운터값을 증가시켜 앞서 말씀드린 클래스를 사용해 저장시킨 후
다시 태깅하면 저장된 순번 데이터를 불러오는 시스템으로 프로그램을 짰는데 다른 단말기로 태깅하면 카운터 값이 증가가 안되고 값이 1로 초기화 되서 나오네요
그리고 같은 단말기에서도 앱을 종료시킨 후 다시 실행시켰을 때 그전 카운터 값에서 +1까지는 증가가 되는데 그 다음 태깅부터는 다시 번호 값이 1로 초기화가 되네요
제가 짠 소스코딩은 아래와 같습니다.
/************************************
* 여기서부턴 NFC 관련 메소드
************************************/
public void onPause() {
super.onPause();
if (mAdapter != null) {
mAdapter.disableForegroundDispatch(this);
}
}
public void onResume() {
super.onResume();
if (mAdapter != null) {
mAdapter.enableForegroundDispatch(this, mPendingIntent, mFilters,
mTechLists);
}
}
// NFC 태그 스캔시 호출되는 메소드2
public void onNewIntent(Intent passedIntent) {
if (passedIntent != null) {
processTag(passedIntent); // processTag 메소드 호출
}
}
// onNewIntent 메소드 수행 후 호출되는 메소드
private void processTag(Intent passedIntent) {
Parcelable[] rawMsgs = passedIntent
.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
if (rawMsgs == null) {
return;
}
NdefMessage[] msgs;
if (rawMsgs != null) {
msgs = new NdefMessage[rawMsgs.length];
for (int i = 0; i < rawMsgs.length; i++) {
msgs[i] = (NdefMessage) rawMsgs[i];
showTag(msgs[i]); // showTag 메소드 호출
}
}
}
private void getPreference() { // 대기번호 카운터 값 불러오기
/*pref = getSharedPreferences("pref", MODE_MULTI_PROCESS);*/
Log.e(TAG, "대기번호 : " + pref.getInt("waitNum",cnt));
}
private void savePreferences() { // 대기번호 카운터 값 저장하기
/*pref = getSharedPreferences("pref", MODE_MULTI_PROCESS);*/
SharedPreferences.Editor editor = pref.edit();
editor.putInt("waitNum", cnt);
editor.commit();
}
private void removePreferences() { // 대기번호 카운터 값 제거하기
/*pref = getSharedPreferences("pref", MODE_MULTI_PROCESS);*/
SharedPreferences.Editor editor = pref.edit();
editor.remove("waitNum");
editor.commit();
}
// NFC 태그 정보를 읽어들이는 메소드
private int showTag(NdefMessage mMessage) {
List<ParsedRecord> records = NdefMessageParser.parse(mMessage);
final int size = records.size();
for (int i = 0; i < size; i++) {
ParsedRecord record = records.get(i);
int recordType = record.getType();
String recordStr = ""; // NFC 태그로부터 읽어들인 텍스트 값
if (recordType == ParsedRecord.TYPE_TEXT) {
recordStr = ((TextRecord) record).getText().toString();
if (recordStr != null) {
getPreference();
cnt++;
removePreferences();
savePreferences();
}
}
}
return size;
}
어떻게 해결해야 할까요 고수형님들 답변부탁드립니다