2011-07-26 59 views
2

我真的讀了10或20個話題,不幸的是我沒有使它工作。我的接收器可以捕獲廣播,但只能通過我的應用程序通過sendBroadcast(intent)發送。我希望它捕捉NFC適配器的廣播。有人將NFC標籤放在我的設備附近,然後我的應用程序應該在瀏覽菜單中啓動或顯示,但這種情況不會發生。即使我的應用程序啓動,並且我將NFC標籤放在設備附近,也無法捕獲它,而在瀏覽菜單中,我會看到其他應用程序,這可以。 我的接收器:NFC廣播問題

public class SomeBroadcastReceiver extends BroadcastReceiver { 
private final String TAG = "SomeBroadcastReceiver"; 
@Override 
public void onReceive(Context context, Intent intent) { 
Log.d(TAG, "Got intent: " + intent);  
} 
}  

我的清單文件:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.example.android.nfc"> 
<uses-permission android:name="android.permission.NFC" /> 
<uses-permission android:name="android.permission.INTERNET" /> 

<application 
    android:icon="@drawable/icon" 
    android:label="@string/app_name"> 

<receiver android:enabled="true" android:name=".broadcast.SomeBroadcastReceiver"> 
    <intent-filter> 
     <action android:name="android.nfc.action.NDEF_DISCOVERED"/> 
     <category android:name="android.intent.category.DEFAULT" /> 

      <action android:name="android.nfc.action.TECH_DISCOVERED"/> 
     <meta-data android:name="android.nfc.action.TECH_DISCOVERED" android:resource="@xml/technologies"/> 

     <action android:name="android.nfc.action.TAG_DISCOVERED"/> 
</intent-filter>   
</receiver> 

<activity android:name=".simulator.FakeTagsActivity" 
    android:theme="@android:style/Theme.NoTitleBar"> 
    <intent-filter> 
     <action android:name="android.intent.action.MAIN" /> 
     <category android:name="android.intent.category.LAUNCHER" /> 
    </intent-filter> 
</activity> 

<activity android:name="TagViewer" 
    android:theme="@android:style/Theme.NoTitleBar"> 
    <intent-filter> 
     <action android:name="android.nfc.action.TAG_DISCOVERED" /> 
     <category android:name="android.intent.category.DEFAULT"/> 
    </intent-filter>   
</activity> 

</application> 
<uses-sdk android:minSdkVersion="10" /> 
<uses-feature android:name="android.hardware.nfc" android:required="true" /> 

在FakeActivity我有這樣的臺詞:

Intent exampleIntent = new Intent("android.nfc.action.NDEF_DISCOVERED"); sendBroadcast(exampleIntent);

當應用reches他們,我接收器捕捉意圖,所以我瘦了k接收器是好的,但也許我錯過了清單中的某些東西?是否有獲得全球廣播的特別許可?或者我應該開始服務還是應該?

+0

您是否找到了解決此問題的解決方案? – Antonio

回答

6

您無法使用BroadcastReceiver捕獲這些意圖,因爲只有活動可以接收NFC意圖。你可以在NFC guide找到更多關於它的信息。

0

我使用的技術描述in this answer - 它提供了與廣播接收機相同的效果。

0

根據此處(https://stackoverflow.com/a/5320694/3736955),您不能捕獲BroadcastReceiver的NFC意圖。處理它的唯一方法是通過活動中的ForegroundDispatch和onNewIntent()函數。當點擊NFC標籤時,它會尋找前臺活動來處理他。