2

このコードを使用して、アプリケーションNFCに読み取りを統合しました。androidタグにプレーンテキストを書き込み、NFCアプリを使用して読み取ると、完全に機能します。今私の要件は、タグURLからNFC読み取ることです。タグから値を読み取るNFCと、自動的にブラウザが開き、読み込まURLれます。コンテンツを読み取ってアプリを開くには、どのような変更が必要ですか?

4

3 に答える 3

1

マニフェストに追加

   <intent-filter>
            <action android:name="android.nfc.action.NDEF_DISCOVERED" />

            <data
                android:host="your host name"
                android:scheme="http" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>

開きたいアクティビティで

于 2015-03-24T04:24:37.877 に答える
0

NFC タグに近づいたときにアプリを起動する場合はフィルターを使用できますが、アプリが実行されている場合はタグについて通知されないことに注意してください。アプリ内で登録する必要があります。

protected void onCreate(Bundle savedInstanceState) {
    ...
    Intent nfcIntent = new Intent(this, getClass());
    nfcIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

    nfcPendingIntent =
            PendingIntent.getActivity(this, 0, nfcIntent, 0);

    IntentFilter tagIntentFilter =
            new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
    try {
        tagIntentFilter.addDataType("text/plain");
        intentFiltersArray = new IntentFilter[]{tagIntentFilter};
    }
    catch (Throwable t) {
        t.printStackTrace();
    }
}

onResume で有効にすることを忘れないでください:

nfcAdpt.enableForegroundDispatch(
            this,
            nfcPendingIntent,
            intentFiltersArray,
            null);
    handleIntent(getIntent());

onPause で登録解除します。

nfcAdpt.disableForegroundDispatch(これ);

.. データは NFC タグの SmartPoster 構造に保存できることに注意してください。この場合、別の方法で読む必要があります。私のブログでは、 SmartPoster などを読むことに関する投稿を見つけることができます。

于 2015-03-26T16:10:04.693 に答える
0

ここでは、返される結果は url のみであり、他のデータは含まれないと想定しているため、次のように変更するだけonPostExecuteです。

@Override
protected void onPostExecute(String result) {
    if (result != null) {
        String url = result;
        Intent i = new Intent(Intent.ACTION_VIEW);
        i.setData(Uri.parse(url));
        startActivity(i);
    }
}

解析結果以外のデータも含まれている場合は、URL のみを取得します。

于 2015-03-23T08:56:35.827 に答える