简体   繁体   English

有没有不使用onNewIntent来获取NFC UID的方法?

[英]Is there a way of getting a NFC UID without using onNewIntent?

I have created an application which gets the NFC UID and changes a text view to the ID however, when i try to do more things with the application, i get an error message. 我创建了一个获取NFC UID并将文本视图更改为ID的应用程序,但是,当我尝试使用该应用程序执行更多操作时,我收到一条错误消息。 For example, if i try to say... if the UID = "001hghg" then change another text view to "success!" 例如,如果我尝试说...如果UID =“ 001hghg”,则将另一个文本视图更改为“成功!”。 Therefore i was wondering if its possible to start the application first, scan the tag and within the onCreate or onResume method, change the text view. 因此,我想知道是否有可能首先启动应用程序,扫描标签并在onCreate或onResume方法内更改文本视图。 Here is the code below: 这是下面的代码:

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

}

Intents are Android's means of inter-process communication. 意图是Android进行进程间通信的手段。 Consequently, being notified about actions external to your applications typically involves notifications through intents. 因此,收到有关应用程序外部操作的通知通常涉及通过意图进行的通知。 A user scanning an NFC tag is an action that happens outside of your application, so you want your application to be somehow notified about that event. 用户扫描NFC标签是在应用程序外部发生的操作,因此您希望以某种方式通知您的应用程序该事件。 In general, the NFC service does this by sending an intent, and your application registers to receive that intent. 通常,NFC服务通过发送意图来实现此目的,然后您的应用程序进行注册以接收该意图。

On Android < 4.4, intents were the only way of receiving a notification on NFC tag events. 在Android <4.4上,意图是接收有关NFC标签事件的通知的唯一方法。 While your app is running, you can either receive them through onNewIntent (intent filters in manifest, foreground dispatch) or through onActivityResult (foreground dispatch). 当您的应用程序运行时,您可以通过onNewIntent (清单,前台分派中的意图过滤器)或onActivityResult (前台分派)接收它们。

Starting with Android 4.4, there is a new reader-mode API. 从Android 4.4开始,有一个新的阅读器模式API。 THis API permits you to register for a specific callback onTagDiscovered to be triggered upon tag detection. 该API允许您注册特定的onTagDiscovered回调,以在检测到标签时触发。

So yes, you can be notified about tag discovery through other callback methods than onNewIntent . 因此,可以的,您可以通过onNewIntent以外的其他回调方法来通知标签发现。 But no, the onCreate method will be the first method invoked when your activity is created. 但是不, onCreate方法将是创建活动时首先调用的方法。 onStart and onResume immediately follow that method. onStartonResume立即遵循该方法。 So there is no way to scan that tag in between starting your activity manually and invokation of these methods. 因此,在手动开始活动和调用这些方法之间没有方法可以扫描该标签。

However, if you activity has been started by an NFC intent, you can get that intent with the activity's getIntent method. 但是,如果您的活动已由NFC意图启动,则可以使用活动的getIntent方法获得该意图。 This also works in any of the methods onCreate , onStart and onResume . 这也可以在任何方法onCreateonStartonResume

The action you are checking (ACTION_TAG_DISCOVERED) is bypassed by ACTION_NDEF_DISCOVERED. 您正在检查的动作(ACTION_TAG_DISCOVERED)被ACTION_NDEF_DISCOVERED绕过。 I worked on a NFC project recently and for some reason every time I would use this action it wouldn't pass (even with the action_tag_discovered filter). 我最近参与了一个NFC项目,由于某种原因,每次我使用该操作都不会通过(即使使用action_tag_discovered过滤器),也由于某种原因。 So I started checking discovery with TECH and NDEF_DISCOVERED only. 因此,我开始仅使用TECH和NDEF_DISCOVERED检查发现。

I don't know if this is of any help, but it kinda solved all my problems 8-) 我不知道这是否有帮助,但可以解决我所有的问题8-)

Maybe you could post the error you are getting? 也许您可以发布您遇到的错误?

And which environment you are using. 以及您使用的环境。 Are you using Android Studio and a phone linked with adb? 您是否正在使用Android Studio和与adb链接的电话?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM