开发者

Basic Java, how to declare these Android functions

I have some documentation that doesn't have any intuitive examples for me

http://developer.android.com/reference/android/nfc/tech/NfcV.html

http://developer.android.com/reference/android/nfc/Tag.html

I need to declare objects of type NfcV and of type Tag , but when I try to do something like NfcV myNFCObject = new NfcV(); the IDE 开发者_开发问答says "constructor NfcV is not visible. So my next try looked like this NfcV myNFCObject = NfcV.getTag(new Tag()); and I get the error "constructor Tag()" is not visible.

So thats where I hit a wall, just from looking at both of the documentation links, I do not see how to declare these objects! How would this be done?

Thanks!


Firstly you must create a class which implements TagTechnology interface. Then you can get tag with it.

import java.io.IOException;

import android.nfc.Tag; 

public class sampleTagTech implements android.nfc.tech.TagTechnology {

public void close() throws IOException {
    // TODO Auto-generated method stub

}

public void connect() throws IOException {
    // TODO Auto-generated method stub

}

public Tag getTag() {
    // TODO Auto-generated method stub
    return null;
}

public boolean isConnected() {
    // TODO Auto-generated method stub
    return false;
}

}

then you can use like this

NfcV nfcv = NfcV.get(new sampleTag().getTag()); 


According to the documentation you can do this:

NfcV object = NfcV.get(myTag)

Also there is no public constructor for myTag (which is why you get an error saying it is not visible it could either be private or protected) per the documentation you provide:

When a tag is discovered, a Tag object is created and passed to a single activity via the EXTRA_TAG extra in an Intent via startActivity(Intent).


When you hold an ISO15693-compliant tag near an NFC-enabled Android device, an intent will be created by the Android system. This intent will contain a handle for the tag. When your app receives the intent, it can retrieve it from there. See http://developer.android.com/guide/topics/nfc/nfc.html#filtering-intents for a good explanantion on how to receive NFC intents in your app.

In the activity that receives the NFC intent, you can get access to NfcV like this:

Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
NfcV nfcV = NfcV.get(tag);
if (nfcV != null) {
  ...
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜