【问题标题】:Is there a way of getting a NFC UID without using onNewIntent?有没有办法在不使用 onNewIntent 的情况下获取 NFC UID?
【发布时间】:2014-03-03 21:33:27
【问题描述】:

我创建了一个应用程序,它获取 NFC UID 并将文本视图更改为 ID,但是,当我尝试对应用程序执行更多操作时,我收到一条错误消息。例如,如果我尝试说...如果 UID =“001hghg”,则将另一个文本视图更改为“成功!”因此我想知道是否可以先启动应用程序,扫描标签并在 onCreate 或 onResume 方法中更改文本视图。下面是代码:

public class MainActivity extends Activity {

    TextView tvUID;

    // list of NFC technologies detected:
    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);

    }

    @Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();

    }

    @Override
    protected void onResume() {
        super.onResume();
        // creating pending intent:
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
        // creating intent receiver for NFC events:
        IntentFilter filter = new IntentFilter();
        filter.addAction(NfcAdapter.ACTION_TAG_DISCOVERED);
        filter.addAction(NfcAdapter.ACTION_NDEF_DISCOVERED);
        filter.addAction(NfcAdapter.ACTION_TECH_DISCOVERED);
        // enabling foreground dispatch for getting intent from NFC event:
        NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
        nfcAdapter.enableForegroundDispatch(this, pendingIntent, new IntentFilter[]{filter}, this.techList);

    } 



    @Override
    protected void onNewIntent(Intent intent) {
        if (intent.getAction().equals(NfcAdapter.ACTION_TAG_DISCOVERED)) {
            tvUID = (TextView) findViewById(R.id.tvUID);
            tvUID.setText(ByteArrayToHexString(intent.getByteArrayExtra(NfcAdapter.EXTRA_ID)));
        }
    } 

    private String ByteArrayToHexString(byte [] inarray) {
        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;
    }

}

【问题讨论】:

    标签: android android-intent nfc uniqueidentifier activity-lifecycle


    【解决方案1】:

    Intent 是 Android 的进程间通信方式。因此,收到有关应用程序外部操作的通知通常涉及通过意图进行的通知。用户扫描 NFC 标签是在您的应用程序之外发生的操作,因此您希望以某种方式通知您的应用程序该事件。通常,NFC 服务通过发送一个意图来实现这一点,然后您的应用注册以接收该意图。

    在 Android onNewIntent(清单中的意图过滤器,前台调度)或onActivityResult(前台调度)接收它们。

    从 Android 4.4 开始,有一个新的阅读器模式 API。此 API 允许您注册特定回调 onTagDiscovered,以便在标签检测时触发。

    所以是的,您可以通过onNewIntent 以外的其他回调方法收到有关标签发现的通知。但是不,onCreate 方法将是创建活动时调用的第一个方法。 onStartonResume 立即遵循该方法。因此,在手动启动活动和调用这些方法之间无法扫描该标签。

    但是,如果您的 Activity 已由 NFC Intent 启动,则可以使用 Activity 的 getIntent 方法获取该 Intent。这也适用于onCreateonStartonResume 的任何方法。

    【讨论】:

    • 嗨,你能给我一些关于如何扫描标签然后进行另一项活动的建议吗?例如,一旦标签被扫描,我可以将该标签 ID 发送到 android 数据库吗?现在,我可以获取 ID,但由于 onNewIntent 阻止了我,因此无法对应用程序执行任何其他操作。谢谢
    • 我真的不明白为什么处理 onNewIntent 中的标签 ID 会“停止”(对您有任何影响)。
    【解决方案2】:

    您正在检查的操作 (ACTION_TAG_DISCOVERED) 被 ACTION_NDEF_DISCOVERED 绕过。我最近在一个 NFC 项目上工作,由于某种原因,每次我使用此操作时它都不会通过(即使使用 action_tag_discovered 过滤器)。所以我开始只使用 TECH 和 NDEF_DISCOVERED 检查发现。

    我不知道这是否有任何帮助,但它解决了我所有的问题 8-)

    也许您可以发布您遇到的错误?

    以及您使用的环境。您使用的是 Android Studio 和与 adb 关联的手机吗?

    【讨论】:

    • 嗨,这段代码运行良好,它给了我 UID 但是我不想使用 onNewIntent,我想要的只是手动启动应用程序然后扫描标签,文本视图变为NFC ID。整个应用程序会将 UID 保存在数据库中,但这将是另一场难题 :(
    猜你喜欢
    • 2019-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-06
    • 2011-06-04
    • 2016-01-10
    相关资源
    最近更新 更多