1

次のことを行うアクティビティが必要な Android アプリケーションを構築しようとしています: ロードしてから、NFC タグが検出されるまで待機します。タグがどのように解析されるか (それがスマート ポスターか URI かなど) はあまり気にしません。私が興味を持っているのは、そのタグの ID だけです。タグとその ID が検出されたら、いくつかの計算を実行してから、待機状態 (アプリケーションが NFC タグの検出を待機している状態) に戻りたいと考えています。

私の問題は、タグの検出によってすべてのコードをトリガーする方法がわからないことです。(アプリケーションは実行中であるため、アプリケーションの優先度の問題ではないことに注意してください。代わりに、タグの検出によってコードがトリガーされ、待機状態に戻るようにします)。

どうもありがとうございました

4

1 に答える 1

8

では、以下のコードです。秘訣は、アクティビティがすべての新しいタグを取得できるように、フォアグラウンド タグ ディスパッチを登録することです。また、フラグ SINGLE_TOP を指定して、アクティビティのアクティブな 1 つのインスタンスが onNewIntent で呼び出されるようにします。ForegroundUtil も投稿します。

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));    

}


}

Foreground-Util (必要に応じてインテント フィルターを変更する必要があります)

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;
}   

}
于 2011-04-04T19:25:06.990 に答える