2011-02-26 2 views
1

다음 작업을 수행해야하는 Android 애플리케이션을 빌드하려고합니다. NFC 태그가 감지 될 때까지로드 된 다음 대기합니다. 태그가 파싱되는 방식 (스마트 포스터 또는 URI 등)에 대해서는별로 신경 쓰지 않습니다. 관심있는 유일한 것은 해당 태그의 ID입니다. 태그와 ID가 감지되면 계산을 수행 한 다음 대기 상태 (응용 프로그램이 NFC 태그를 검색하기 위해 대기중인 상태)로 돌아가겠습니다.NFC가있는 Android 애플리케이션

제 문제는 태그를 감지하여 모든 코드를 트리거하는 방법을 알아낼 수 없다는 것입니다. (응용 프로그램이 실행 중이므로 응용 프로그램 우선 순위에 문제가 없으므로 대신 태그를 감지하여 코드를 트리거 한 다음 대기 상태로 되돌리려합니다.

고맙습니다.

+0

(당신은 당신의 요구에 맞게 텐트 필터를 수정 shoudl) 기계적 인조 인간/ – ThomasRS

답변

8

다음은 코드입니다. 트릭은 전경 태그 발송을 등록하여 활동이 모든 새 태그를 가져 오는 것입니다. 또한 SINGLE_TOP 플래그를 지정하여 onNewIntent로 활동 중 하나의 인스턴스가 호출되도록하십시오. ForegroundUtil도 게시합니다. http://code.google.com/p/ndef-tools-for- :

public class DashboardActivity extends Activity { 

NFCForegroundUtil nfcForegroundUtil = null; 

private TextView info; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    info = (TextView)findViewById(R.id.info); 

    nfcForegroundUtil = new NFCForegroundUtil(this); 


} 

public void onPause() { 
    super.onPause(); 
    nfcForegroundUtil.disableForeground(); 
} 

public void onResume() { 
    super.onResume(); 
    nfcForegroundUtil.enableForeground(); 

    if (!nfcForegroundUtil.getNfc().isEnabled()) 
    { 
     Toast.makeText(getApplicationContext(), "Please activate NFC and press Back to return to the application!", Toast.LENGTH_LONG).show(); 
     startActivity(new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS)); 
    } 

} 

public void onNewIntent(Intent intent) { 
    Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); 
    info.setText(NFCUtil.printTagDetails(tag));  

} 


} 

전경 - 백분율 여기에 샘플 작업

public class NFCForegroundUtil { 

private NfcAdapter nfc; 


private Activity activity; 
private IntentFilter intentFiltersArray[]; 
private PendingIntent intent; 
private String techListsArray[][]; 

public NFCForegroundUtil(Activity activity) { 
    super(); 
    this.activity = activity; 
    nfc = NfcAdapter.getDefaultAdapter(activity.getApplicationContext()); 

    intent = PendingIntent.getActivity(activity, 0, new Intent(activity, 
      activity.getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0); 

    IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED); 

    try { 
     ndef.addDataType("*/*"); 
    } catch (MalformedMimeTypeException e) { 
     throw new RuntimeException("Unable to speciy */* Mime Type", e); 
    } 
    intentFiltersArray = new IntentFilter[] { ndef }; 

    techListsArray = new String[][] { new String[] { NfcA.class.getName() } }; 
    //techListsArray = new String[][] { new String[] { NfcA.class.getName(), NfcB.class.getName() }, new String[] {NfcV.class.getName()} }; 
} 

public void enableForeground() 
{ 
    Log.d("demo", "Foreground NFC dispatch enabled"); 
    nfc.enableForegroundDispatch(activity, intent, intentFiltersArray, techListsArray);  
} 

public void disableForeground() 
{ 
    Log.d("demo", "Foreground NFC dispatch disabled"); 
    nfc.disableForegroundDispatch(activity); 
} 

public NfcAdapter getNfc() { 
    return nfc; 
} 

}