- 论坛徽章:
- 0
|
来源官网,总结用.
指定NFC功能- /**通过添加<uses-feature>标签作为一个<manifest>标签的子标签,指定我们的应用程序使用NFC。
- 设置android:required属性字段为true,使得我们的应用程序只有在NFC可以使用时才能运行。
-
- 下面的代码展示了如何指定<uses-feature>标签:*/
-
- <uses-feature
- android:name="android.hardware.nfc"
- android:required="true" />
- /**注意,如果应用程序将NFC作为一个可选的功能,期望在NFC不可使用时程序还能继续执行,
- 我们就应该将android:required属性字段设为false,然后在代码中测试NFC的可用性。*/
复制代码 测试设备是否支持Android Beam文件传输- //应使用以下标签使得在Manifest清单文件中指定NFC是可选的:
-
- <uses-feature android:name="android.hardware.nfc" android:required="false" />
-
- /**如果设置了android:required="false",则我们必须在代码中测试设备是否支持NFC和
- Android Beam文件传输。
-
- 为在代码中测试是否支持Android Beam文件传输,我们先通过PackageManager.hasSystemFeature()
- 和参数FEATURE_NFC测试设备是否支持NFC。下一步,通过SDK_INT的值测试系统版本是否支持
- Android Beam文件传输。如果设备支持Android Beam文件传输,
- 那么获得一个NFC控制器的实例,它能允许我们与NFC硬件进行通信*/
-
- 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);
- ...
- }
- }
- ...
- }
复制代码 创建一个提供文件的回调函数- /**一旦确认了设备支持Android Beam文件传输,那么可以添加一个回调函数,
- 当Android Beam文件传输监测到用户希望向另一个支持NFC的设备发送文件时,
- 系统就会调用该函数。在该回调函数中,返回一个Uri对象数组,
- Android Beam文件传输会将URI对应的文件拷贝给要接收这些文件的设备。
-
- 要添加这个回调函数,需要实现NfcAdapter.CreateBeamUrisCallback接口
- 和它的方法:createBeamUris(),下面是一个例子:*/
-
- 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;
- }
- }
- ...
- }
- /**
- 一旦实现了这个接口,通过调用setBeamPushUrisCallback()将回调函数提供给
- Android Beam文件传输。下面是一个例子:*/
-
- 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对象数组通过应用程序的NfcAdapter实例,
- 直接提供给NFC框架。如果能在NFC触碰事件发生之前,定义这些URI,
- 那么可以选择使用这个方法。更多关于这个方法的知识,
- 请参考:NfcAdapter.setBeamPushUris()。/*
复制代码 指定要发送的文件- /**为了将一或多个文件发送给其他支持NFC的设备,需要为每一个文件获取一个
- File URI(一个具有文件格式(file scheme)的URI),
- 然后将它们添加至一个Uri对象数组中。此外,要传输一个文件,
- 我们必须也拥有该文件的读权限。下例展示了如何根据文件名获取其File URI
- ,然后将URI添加至数组当中:*/
-
- /*
- * 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.");
- }
复制代码 |
|