2012-03-19 5 views
1

앱의 버튼을 클릭 할 때 NFC 태그를 읽으려고합니다. 현재 기본 모드 (Nexus 휴대 전화에 설치된 태그 앱)에서 태그를 감지 할 수 있습니다. 하지만 난 내 태그 시작하려는 통해 활동 선택기를 표시 얻을 수 아니다Android NFC 시작 화면

public class NFC_button extends Activity 
{ 

protected IntentFilter ifilter ; 
private NfcAdapter adapter; 

private BroadcastReceiver receiver = new BroadcastReceiver() 
{ 

    @Override 
    public void onReceive(Context context, Intent intent) 
    { 

     if(NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) 
     { 
      Parcelable[] messages = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES); 
      NdefMessage[] ndefmessages; 
      if(messages != null) 
      { 
       ndefmessages = new NdefMessage[messages.length]; 

       for(int i = 0;i<messages.length;i++) 
       { 
        ndefmessages[i] = (NdefMessage)messages[i]; 
       } 



      } 

     } 

    } 
}; 

@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    adapter=NfcAdapter.getDefaultAdapter(this); 


    ifilter = new IntentFilter(); 
    ifilter.addAction("android.nfc.action.NDEF_DISCOVERED"); 
    ifilter.addCategory("android.intent.category.LAUNCHER"); 

} 



@Override 
protected void onResume() { 
    registerReceiver(receiver, ifilter); 

super.onResume(); 
} 




} 

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.nfc.example" 
android:versionCode="1" 
android:versionName="1.0" > 

<uses-permission android:name="android.permission.NFC"/> 
<uses-feature android:name="android.hardware.nfc" android:required="true"/> 

<uses-sdk android:minSdkVersion="10"/> 

<application 

    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" > 
    <activity 
     android:name=".NFC_ExampleActivity" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 

    <activity android:name=".NFC_button"> 

     </activity> 

</application> 
나는 BroadcastReciver을 생각하지 않는이 태그를 읽을 수있는 올바른 방법 모두의

답변

1

우선 .

android.intent.category.LAUNCHER 

하지만 올바른 카테고리가 있어야한다 :

android.intent.category.DEFAULT 

난 당신의 매니페스트에 인 텐트 필터를 추가하는 것이 제안 그리고 내가 볼 다른 실수는 의도 필터 카테고리를 가지고 있다는 것입니다 당신이 원하는 활동은이 같은 태그를 터치하면 시작 :

<activity android:name=".NFC_button"> 
<intent-filter > 
    <action android:name="android.nfc.action.NDEF_DISCOVERED"/> 
    <category android:name="android.intent.category.DEFAULT"/> 
</intent-filter> 
</activity> 

을하고 NFC_butt의에서 onCreate에 브로드 캐스트 리시버의 onReceive 방법이 코드를 이동 활동.

BroadcastReceiver를 사용하려는 특별한 이유가 없으면 태그 읽기 문제가 해결됩니다.

+0

왜 BroadcastReceiver가 태그를 읽는 올바른 방법이라고 생각하지 않았는지 설명 할 수 있습니까? 내 유스 케이스를 고려해보십시오. 태그에 데이터를 쓰고 있습니다. 사용자가 데이터를 준비한 다음 태그가 감지되면 데이터를 쓸 장치의 가까이에 태그를 놓으십시오. 사용자가 다른 활동을하고있어 데이터를 쓸 준비가되기 때문에 활동을 시작하고 싶지 않습니다 (UI 변경). – wsgeorge