【问题标题】:Getting NFC Unique ID within the oncreate method在 oncreate 方法中获取 NFC 唯一 ID
【发布时间】:2013-12-09 04:05:44
【问题描述】:

我正在尝试在 on create 方法中获取 NFC 标签的唯一 ID。我已经使用名为“onNewIntent”的覆盖方法成功完成了这项工作,但是我想在 oncreate 方法中获取它。这是我尝试过的,我手机上的应用程序在启动时崩溃了。请任何人都可以帮忙。谢谢。

public class MainActivity extends Activity {

    TextView uid;

    private final String[][] techList = new String[][] {
        new String[] {
            NfcA.class.getName(),
            NfcB.class.getName(),
            NfcF.class.getName(),
            NfcV.class.getName(),
            NdefFormatable.class.getName(),
            TagTechnology.class.getName(),
            IsoDep.class.getName(),
            MifareClassic.class.getName(),
            MifareUltralight.class.getName(), 
            Ndef.class.getName()
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        uid = (TextView) findViewById(R.id.UID);
        Intent i = new Intent();
        IntentFilter filter = new IntentFilter();
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
        filter.addAction(NfcAdapter.ACTION_TAG_DISCOVERED);
        NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
        nfcAdapter.enableForegroundDispatch(this, pendingIntent, new IntentFilter[]{filter}, this.techList);
        if (i.getAction().equals(NfcAdapter.ACTION_TAG_DISCOVERED)) {
            ((TextView)findViewById(R.id.UID)).setText(ByteArrayToHexString(i.getByteArrayExtra(NfcAdapter.EXTRA_ID)));
        }
    }

    private String ByteArrayToHexString(byte [] inarray) { //converts byte arrays to string
        int i, j, in;
        String [] hex = {"0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"};
        String out= "";

        for(j = 0 ; j < inarray.length ; ++j) 
            {
            in = (int) inarray[j] & 0xff;
            i = (in >> 4) & 0x0f;
            out += hex[i];
            i = in & 0x0f;
            out += hex[i];
            }
        return out;
    }
}

【问题讨论】:

  • 发布 logcat 输出。
  • 好的,基本上我创建了一个新的意图。我从设备中获取了默认的 NFC 硬件。如果发现标签,则应将 textview 更改为 NFC 标签的 ID

标签: android nfc uniqueidentifier oncreate


【解决方案1】:

好的,所以基本上我创建了一个新意图。我从设备中获取了默认的 NFC 硬件。如果发现标签,则应将 textview 更改为 NFC 标签的 ID

为此,您所要做的就是设置新文本并在onNewIntent() 中调用invalidate(),这样TextView 就会用新文本重新绘制:

package com.example.changetext;

import android.app.Activity;
import android.content.Intent;
import android.nfc.NfcAdapter;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends Activity {
    private TextView mNfcAdapterUid;

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

        mNfcAdapterUid = (TextView)findViewById(R.id.nfc_adapter_uid);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) {
            mNfcAdapterUid.setText(ByteArrayToHexString(intent.getByteArrayExtra(NfcAdapter.EXTRA_ID)));
            mNfcAdapterUid.invalidate();
        }
    }

    private String ByteArrayToHexString(byte[] inarray) { // converts byte arrays to string
        int i, j, in;
        String[] hex = {
                "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"
        };
        String out = "";

        for (j = 0; j < inarray.length; ++j) {
            in = inarray[j] & 0xff;
            i = (in >> 4) & 0x0f;
            out += hex[i];
            i = in & 0x0f;
            out += hex[i];
        }
        return out;
    }
}

希望这会有所帮助。

【讨论】:

  • 您好,感谢您的帮助,我已经使用 onNewIntent 完成了代码,但是我需要在 onCreate 方法中获取 UID。
【解决方案2】:

您似乎在这里混合了两个概念:在应用清单中注册的 NFC 意图和前台调度。

根据 NFC 事件自动启动应用:

此机制要求您在应用清单中注册意图过滤器,例如

<activity ...>
    <intent-filter>
        <action android:name="android.nfc.action.TECH_DISCOVERED"/>
    </intent-filter>
    <meta-data android:name="android.nfc.action.TECH_DISCOVERED"
               android:resource="@xml/nfc_tech_filter" />
</activity>

然后您的活动将在检测到标签时自动启动。 (注意:避免对这种类型的调度使用TAG_DISCOVERED 意图,因为该意图(在清单中使用时)仅作为后备!)

现在,如果您的应用已启动,您可以在 onCreate() 方法中检测它是否是由 TECH_DISCOVERED 意图启动的:

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

    Intent i = getIntent();  // get the intent that started us
    if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(i.getAction())) {
        uid.setText(ByteArrayToHexString(i.getByteArrayExtra(NfcAdapter.EXTRA_ID)));
    }
}

前台调度系统:

前台调度系统用于注册您的当前前台活动,以便收到有关 NFC 事件的通知(并获得最高的接收优先级)。您可以通过enableForegroundDispatch() 方法注册相关意图(TAG_DISCOVERED 在这种情况下很好)来使用它。收到 NFC 事件后,您的活动将通过onNewIntent() 收到通知。

但是请记住,您只能在您的 Activity 是前台 Activity 时注册前台调度(即您必须在 onResume() 注册,并且必须在 onPause() 取消注册),否则您的应用将阻塞NFC 事件或崩溃(取决于 Android 版本)。所以永远不要onCreate()中使用enableForegroundDispatch()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-11
    • 2011-08-03
    • 1970-01-01
    • 2019-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-13
    相关资源
    最近更新 更多