簡體   English   中英

如何從 nfc 標簽讀取多個有效負載並將有效負載存儲到數組列表中?

[英]How to read multiple payload from nfc tags and store the payloads into an arraylist?

嗨 Stackers 我還是編碼新手,需要一些幫助。 現在我有兩個 NFC 標簽,兩個標簽都使用不同的地理坐標(經度、緯度)存儲。 目前,我能夠從 NFC 標簽讀取有效載荷。 我想要的是將來自兩個 NFC 標簽的有效載荷存儲到一個數組列表中。 當我讀取第一個標簽時,它確實將有效負載存儲到數組列表中,但是當讀取第二個標簽時,有效負載將覆蓋前一個有效負載。

安卓清單:

<uses-permission android:name="android.permission.NFC" />

<uses-feature
    android:name="android.hardware.nfc"
    android:required="true" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".arraylist"></activity>
    <activity
        android:name=".MainActivity"
        android:launchMode="singleInstance">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

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

            <data
                android:host="*"
                android:scheme="geo" />
        </intent-filter>
    </activity>
</application>

主要活動:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    nfcAdapter = NfcAdapter.getDefaultAdapter(this);
    if (nfcAdapter == null) {
        Toast.makeText(this, "nfc not supported", Toast.LENGTH_SHORT).show();
        finish();
        return;
    }
    if (!nfcAdapter.isEnabled()) {
        startActivity(new Intent("android.settings.NFC_SETTINGS"));
        Toast.makeText(this, "nfc not yet open", Toast.LENGTH_SHORT).show();
    }

    mPendingIntent = PendingIntent.getActivity(this,0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP),0 );


}

private void readIntent(Intent intent){
    list = new ArrayList<>();
    Parcelable[] parcelables = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
    for(int i=0; i<parcelables.length; i ++){
        NdefMessage message =(NdefMessage)parcelables[i];
        NdefRecord[] records = message.getRecords();
        for(int j=0; j<records.length; j++){
            NdefRecord record = records[j];
            byte[] original = record.getPayload();
            byte[] value = Arrays.copyOfRange(original,0,original.length);
            String payload = new String(value);

            if(list != null){
                list.add(payload);
                System.out.println(list);
            }

        }
    }
}



@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    readIntent(intent);

}

@Override
protected void onResume() {
    super.onResume();

    nfcAdapter.enableForegroundDispatch(this, mPendingIntent, null, null);
}

@Override
protected void onPause() {
    super.onPause();
    nfcAdapter.disableForegroundDispatch(this);
}

這是我的輸出:I/System.out: [geo:1.523534,103.633690]

但我的期望輸出是:[geo:1.523534, 103.633690, geo:1.5364496, 103.656321]

那么如何才能實現第二個標簽有效載荷不會覆蓋前一個有效載荷呢?

這次的問題是,您沒有像在之前的類似問題中那樣創建 Fragment 的new實例,而是在每次讀取卡片時創建一個new的數組列表,從而取消引用第一個列表對象以使其成為被垃圾收集刪除。

這是一個經典的變量初始化問題。

每次讀取卡片時都會調用readIntent 因此,刪除創建新列表的 `readIntent 的第一行。

例如

private void readIntent(Intent intent){
    Parcelable[] parcelables = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);

....

然后在您的類定義中只初始化一次數組列表。

例如

public class MainActivity extends AppCompatActivity {

    private ArrayList<String> list = new ArrayList();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM