如何使用Android NFC传输文件

时间:2014-10-30 12:24:10

标签: android file nfc intentfilter transfer

我正在尝试使用Android Beam在应用之间传输大文件。发送部分运行良好,文件出现在' beam /'目录。通知状态栏显示"梁完成"。但是,在将文件重命名为beam /目录后,接收应用程序不会收到通知,并且onNewIntent()永远不会在接收端被调用。 intent-filter我错过了什么?还可以在使用createBeamUris()时指定Android应用程序记录吗? TIA

// sending app
nfcAdapter.setBeamPushUrisCallback(this, this);
...
@Override
public Uri[] createBeamUris(NfcEvent event) {    // send files
    File dir = Environment.getExternalStorageDirectory();
    File file = new File(dir, "test.txt");
    file.setReadable(true, false);    // readable=true, ownerOnly=false
    return new Uri[] { Uri.fromFile(file) };
}

我的Manifest.xml:

<activity
    android:name=".BeamDemo2"
    android:label="@string/app_name"
    android:launchMode="singleTop" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:scheme="file" />
        <data android:mimeType="*/*" />
        <data android:pathPattern=".*\\.txt" />
        <data android:host="*" />
    </intent-filter>
    <intent-filter>
        <action android:name="android.nfc.action.NDEF_DISCOVERED" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:mimeType="application/com.example.beamdemo2" />
    </intent-filter>
</activity>

我也尝试过enableForegroundDispatch():

@Override
public void onResume() {
    super.onResume();
    try {
        PendingIntent mPendingIntent = PendingIntent.getActivity(this, 0,
            new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);

        IntentFilter ndefIntent = new IntentFilter("android.intent.action.VIEW");
        ndefIntent.addCategory("android.intent.category.DEFAULT");
        ndefIntent.addDataType("*/*");
        IntentFilter[]mIntentFilters = new IntentFilter[] { ndefIntent };
        String[][] mNFCTechLists = new String[][] { new String[] { NfcF.class.getName() } };
        mNfcAdapter.enableForegroundDispatch(this, mPendingIntent, mIntentFilters, mNFCTechLists);
    } catch (Exception e) {
        Log.e("onResume", e.toString());
    }
}

1 个答案:

答案 0 :(得分:0)

简短的回答,没有答案:-(。梁完成后,需要下拉&#34;梁完成&#34;通知并选择&#34;查看文件&#34;。这会导致选择出现,然后将VIEW意图发送给活动。我希望避免这个手动步骤,但这似乎是不可避免的。我也想避免选择器为VIEW选择一个应用程序,但是这似乎也是不可避免的.EnableForegroundDispatch似乎只适用于android.nfc.action.NDEF_DISCOVERED。

选择应用后,事情按预期工作。我看到onNewIntent(),然后是onResume()。一种奇怪,接受的意图似乎坚持并不断重现。在onResume()处理VIEW意图后,我不得不通过调用setIntent(null)来杀死它。

@Override
public void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    setIntent(intent);
}

@Override
public void onResume() {
    super.onResume();
    Intent intent = getIntent();
    if (Intent.ACTION_VIEW.equals(intent.getAction())) {
        processIntent(intent);  // 
        setIntent(null);        // kill this event
    }
}