- 論壇徽章:
- 0
|
來(lái)源官網(wǎng),總結(jié)用.
指定NFC功能- /**通過(guò)添加<uses-feature>標(biāo)簽作為一個(gè)<manifest>標(biāo)簽的子標(biāo)簽,指定我們的應(yīng)用程序使用NFC。
- 設(shè)置android:required屬性字段為true,使得我們的應(yīng)用程序只有在NFC可以使用時(shí)才能運(yùn)行。
-
- 下面的代碼展示了如何指定<uses-feature>標(biāo)簽:*/
-
- <uses-feature
- android:name="android.hardware.nfc"
- android:required="true" />
- /**注意,如果應(yīng)用程序?qū)FC作為一個(gè)可選的功能,期望在NFC不可使用時(shí)程序還能繼續(xù)執(zhí)行,
- 我們就應(yīng)該將android:required屬性字段設(shè)為false,然后在代碼中測(cè)試NFC的可用性。*/
復(fù)制代碼 測(cè)試設(shè)備是否支持Android Beam文件傳輸- //應(yīng)使用以下標(biāo)簽使得在Manifest清單文件中指定NFC是可選的:
-
- <uses-feature android:name="android.hardware.nfc" android:required="false" />
-
- /**如果設(shè)置了android:required="false",則我們必須在代碼中測(cè)試設(shè)備是否支持NFC和
- Android Beam文件傳輸。
-
- 為在代碼中測(cè)試是否支持Android Beam文件傳輸,我們先通過(guò)PackageManager.hasSystemFeature()
- 和參數(shù)FEATURE_NFC測(cè)試設(shè)備是否支持NFC。下一步,通過(guò)SDK_INT的值測(cè)試系統(tǒng)版本是否支持
- Android Beam文件傳輸。如果設(shè)備支持Android Beam文件傳輸,
- 那么獲得一個(gè)NFC控制器的實(shí)例,它能允許我們與NFC硬件進(jìn)行通信*/
-
- public class MainActivity extends Activity {
- ...
- NfcAdapter mNfcAdapter;
- // Flag to indicate that Android Beam is available
- boolean mAndroidBeamAvailable = false;
- ...
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- ...
- // NFC isn't available on the device
- if (!PackageManager.hasSystemFeature(PackageManager.FEATURE_NFC)) {
- /*
- * Disable NFC features here.
- * For example, disable menu items or buttons that activate
- * NFC-related features
- */
- ...
- // Android Beam file transfer isn't supported
- } else if (Build.VERSION.SDK_INT <
- Build.VERSION_CODES.JELLY_BEAN_MR1) {
- // If Android Beam isn't available, don't continue.
- mAndroidBeamAvailable = false;
- /*
- * Disable Android Beam file transfer features here.
- */
- ...
- // Android Beam file transfer is available, continue
- } else {
- mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
- ...
- }
- }
- ...
- }
復(fù)制代碼 創(chuàng)建一個(gè)提供文件的回調(diào)函數(shù)- /**一旦確認(rèn)了設(shè)備支持Android Beam文件傳輸,那么可以添加一個(gè)回調(diào)函數(shù),
- 當(dāng)Android Beam文件傳輸監(jiān)測(cè)到用戶希望向另一個(gè)支持NFC的設(shè)備發(fā)送文件時(shí),
- 系統(tǒng)就會(huì)調(diào)用該函數(shù)。在該回調(diào)函數(shù)中,返回一個(gè)Uri對(duì)象數(shù)組,
- Android Beam文件傳輸會(huì)將URI對(duì)應(yīng)的文件拷貝給要接收這些文件的設(shè)備。
-
- 要添加這個(gè)回調(diào)函數(shù),需要實(shí)現(xiàn)NfcAdapter.CreateBeamUrisCallback接口
- 和它的方法:createBeamUris(),下面是一個(gè)例子:*/
-
- public class MainActivity extends Activity {
- ...
- // List of URIs to provide to Android Beam
- private Uri[] mFileUris = new Uri[10];
- ...
- /**
- * Callback that Android Beam file transfer calls to get
- * files to share
- */
- private class FileUriCallback implements
- NfcAdapter.CreateBeamUrisCallback {
- public FileUriCallback() {
- }
- /**
- * Create content URIs as needed to share with another device
- */
- @Override
- public Uri[] createBeamUris(NfcEvent event) {
- return mFileUris;
- }
- }
- ...
- }
- /**
- 一旦實(shí)現(xiàn)了這個(gè)接口,通過(guò)調(diào)用setBeamPushUrisCallback()將回調(diào)函數(shù)提供給
- Android Beam文件傳輸。下面是一個(gè)例子:*/
-
- public class MainActivity extends Activity {
- ...
- // Instance that returns available files from this app
- private FileUriCallback mFileUriCallback;
- ...
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- ...
- // Android Beam file transfer is available, continue
- ...
- mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
- /*
- * Instantiate a new FileUriCallback to handle requests for
- * URIs
- */
- mFileUriCallback = new FileUriCallback();
- // Set the dynamic callback for URI requests.
- mNfcAdapter.setBeamPushUrisCallback(mFileUriCallback,this);
- ...
- }
- ...
- }
- /**
- Note:我們也可以將Uri對(duì)象數(shù)組通過(guò)應(yīng)用程序的NfcAdapter實(shí)例,
- 直接提供給NFC框架。如果能在NFC觸碰事件發(fā)生之前,定義這些URI,
- 那么可以選擇使用這個(gè)方法。更多關(guān)于這個(gè)方法的知識(shí),
- 請(qǐng)參考:NfcAdapter.setBeamPushUris()。/*
復(fù)制代碼 指定要發(fā)送的文件- /**為了將一或多個(gè)文件發(fā)送給其他支持NFC的設(shè)備,需要為每一個(gè)文件獲取一個(gè)
- File URI(一個(gè)具有文件格式(file scheme)的URI),
- 然后將它們添加至一個(gè)Uri對(duì)象數(shù)組中。此外,要傳輸一個(gè)文件,
- 我們必須也擁有該文件的讀權(quán)限。下例展示了如何根據(jù)文件名獲取其File URI
- ,然后將URI添加至數(shù)組當(dāng)中:*/
-
- /*
- * Create a list of URIs, get a File,
- * and set its permissions
- */
- private Uri[] mFileUris = new Uri[10];
- String transferFile = "transferimage.jpg";
- File extDir = getExternalFilesDir(null);
- File requestFile = new File(extDir, transferFile);
- requestFile.setReadable(true, false);
- // Get a URI for the File and add it to the list of URIs
- fileUri = Uri.fromFile(requestFile);
- if (fileUri != null) {
- mFileUris[0] = fileUri;
- } else {
- Log.e("My Activity", "No File URI available for file.");
- }
復(fù)制代碼 |
|