【问题标题】:How to transfer files with Android NFC如何使用 Android NFC 传输文件
【发布时间】:2014-12-26 11:44:04
【问题描述】:

我正在尝试使用 Android Beam 在应用程序之间传输大文件。发送部分运行良好,文件出现在“beam/”目录中。通知状态栏显示“Beam 完成”。但是,在文件重命名到 beam/ 目录后,接收应用程序不会收到通知,并且 onNewIntent() 永远不会在接收端被调用。意图过滤器缺少什么?是否可以在使用 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());
    }
}

【问题讨论】:

    标签: android file nfc intentfilter transfer


    【解决方案1】:

    简答,没有答案 :-(。Beam 完成后,需要下拉“Beam Complete”通知并选择“View File”。这会导致出现一个选择器,然后是一个 VIEW Intent调度到活动。我希望避免这个手动步骤,但这似乎是不可避免的。我也想避免选择器为 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
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2020-07-03
      • 1970-01-01
      • 2015-09-02
      • 1970-01-01
      • 1970-01-01
      • 2019-09-25
      • 1970-01-01
      • 1970-01-01
      • 2012-12-12
      相关资源
      最近更新 更多