2014-10-30 56 views
0

我試圖使用Android Beam在應用程序之間傳輸大文件。發送部分運行良好,文件出現在'beam /'目錄中。通知狀態欄顯示「Beam complete」。但是,在文件重命名爲beam /目錄並且onNewIntent()永遠不會在接收端調用後,接收應用程序不會收到通知。我意圖過濾器丟失了什麼?還有可能在使用createBeamUris()時指定Android應用程序記錄? TIA如何使用Android NFC傳輸文件

// 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()); 
    } 
} 

回答

0

簡短的回答,沒有答案:-(梁完成後。 ,需要下拉「Beam Complete」通知並選擇「View File」,這會導致Chooser出現,然後將一個VIEW intent分配給該活動。我希望避免這個手動步驟,但似乎無法避免即我也想避免Chooser爲VIEW選擇一個應用程序,但這似乎也不可避免。 EnableForegroundDispatch似乎只適用於android.nfc.action.NDEF_DISCOVERED。

選擇應用程序後,事情按預期工作。我看到onNewIntent(),然後是onResume()。一個奇怪的是,收到的意圖似乎堅持並不斷重現。在onResume()中處理VIEW intent後,我不得不通過調用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 
    } 
}