Android NFC,在onCreate 中做空检查? [英] Android NFC, do a null check in onCreate?

查看:19
本文介绍了Android NFC,在onCreate 中做空检查?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有代码来初始化我的 NFC 适配器,但我不确定如何在 onCreate 期间初始化变量.无论 NFC TAG 是否在附近,都可以初始化应用程序,即.如果有人只是打开了应用程序.所以当我进入这一行 NfcV nfcMessage = NfcV.get(new TagGet().getTag()); 它崩溃,因为它是空的,如果你只是加载应用程序,那里没有标签靠你自己.我如何在此处检查 != null 我不确定要检查哪个部分或如何构建此代码.

I have code to initialize my NFC adapter, but I am not sure how to intialize the variables during the onCreate. The app can be initialized whether an NFC TAG is in proximity or not, ie. if someone simply opened the app. So when I get down to this line NfcV nfcMessage = NfcV.get(new TagGet().getTag()); it crashes because it is null, there is no tag there if you just load the app on your own. How do I check here for != null I'm not sure which part to check or how to structure this code.

结果是我想读取标签的内容,如果存在标签.否则只需加载布局并等待 NfcV 标签被扫描.

The result is that I want to read the contents of the tag IFF a tag is present. Otherwise just load the layout and wait for a NfcV tag to be scanned.

// Setup an intent filter for all MIME based dispatches
    IntentFilter nfcv = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
    try {
        nfcv.addDataType("*/*");
    } catch (MalformedMimeTypeException e) {
        throw new RuntimeException("fail", e);
    }
    mFilters = new IntentFilter[] {
            nfcv,
    };

    // Setup a tech list for all NfcF tags
    mTechLists = new String[][] { new String[] { NfcV.class.getName() } };

    //mAdapter.enableForegroundDispatch(this, mPendingIntent, mFilters, mTechLists);

    NfcV nfcMessage = NfcV.get(new TagGet().getTag());

    byte[] data = new byte[2048]; //tag length can't be any larger
    String value = "";
    try {
        data = nfcMessage.transceive(new byte[2048]);
        value = new String(data);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();

推荐答案

Hear 是我制作第一个 Nfc 应用程序的基本方式的代码大纲.我尝试替换值(我使用 nfcA 而不是 nfcV)以便您可以更好地阅读它.这是一个非常基本的结构,您需要用自己的代码填充部分,但它应该能让您很好地了解 NFC 应用的结构如何工作.

Hear is a code outline of the basic way I made my first Nfc app. I tried to replace values (I used nfcA instead of nfcV) so you could read it better. It is a very basic structure, and you would need to fill in parts with your own code, but It should give you a good idea of how the structure of an NFC app can work.

public class Android_nfc_ibox extends Activity implements Runnable {




NfcAdapter mNfcAdapter;
private String[][] mTechLists;
PendingIntent pendingIntent;
Tag tag;
NfcA mTag;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Initialize the NFC adapter
    mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
    if (mNfcAdapter != null) {
        dialog_text.append("Tap an NFC tag for access

");
    } else {
        dialog_text.append("This phone is not NFC enabled

");
    }

    // Create the PendingIntent object which will contain the details of the tag that has been scanned
    pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);

    // Setup a tech list for all desired tag types
    mTechLists = new String[][] { new String[] { NfcA.class.getName() } };

}

    /** Re-enable the tag dispatch if the app is in the foreground */
   @Override
    public void onResume() {
        super.onResume();
        if (mNfcAdapter != null) mNfcAdapter.enableForegroundDispatch(this, pendingIntent, null, mTechLists);
    }

   /** Disable the tag dispatch when the app is no longer in the foreground */
    @Override
    public void onPause() {
        super.onPause();
        if (mNfcAdapter != null) mNfcAdapter.disableForegroundDispatch(this);
    }

    /** A tag has been discovered */
    @Override 
    public void onNewIntent(Intent intent){

        // get the tag object for the discovered tag
        Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);

        // try and get the MifareUltralight instance for this tag
        mTag = NfcV.get(tag);

        // if null then this wasn't a NfcV tag so wait for next time
        if(mTag == null){
            dialog_text.append("Not a Nfc V tag

");
        }

           // Start the tag communications thread
           Thread myThread = new Thread(this);
           myThread.start();

        }
    }

   // (we could create other threads for other types of tags)
   public void run(){
       // try to connect to the Nfc V tag
       try{

           mTag.connect();
       }catch(IOException e){
            //handle the error here
       }

       //this will send raw data
       //send the values you want in the byte[]
       //just add the raw hex values with commas
       //pageBuffer is an array that will hold the response
       try{
           pageBuffer = mTag.transceive(new byte[] {0x11, 0x24, 0x11});
       }catch(IOException e){
            //handle error here
       }
    } 

}

这篇关于Android NFC,在onCreate 中做空检查?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆