다음은 코드입니다. 트릭은 전경 태그 발송을 등록하여 활동이 모든 새 태그를 가져 오는 것입니다. 또한 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;
}
}
(당신은 당신의 요구에 맞게 텐트 필터를 수정 shoudl) 기계적 인조 인간/ – ThomasRS