2012-05-21 1 views
3

내 Android 앱에는 정보 활동을위한 주요 활동과 NFC 수신 활동이 있습니다.NFC 태그에서 Android 시작 활동을 여러 번 수행

앱을 처음 시작하면 NFC 태그를 여러 번 읽을 수 있습니다. 매번 새로운 활동을 시작하고 정보를 표시합니다.

앱을 닫았지만 휴대 전화가 NFC 태그로 이동하면 처음 nFC 태그 활동이 표시되지만 다른 태그에는 다시 응답하지 않습니다.

내가 뭘 잘못하고 있니?! 두 번째 활동에 대한

매니페스트 부분과 코드 :

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

<application 
android:icon="@drawable/aaa" 
android:label="@string/app_name" 
    android:theme="@android:style/Theme.NoTitleBar"> 

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

<activity 
    android:name=".TagDiscoveredActivity" 
    android:screenOrientation="portrait"> 
    <intent-filter > 
     <action android:name="android.nfc.action.TECH_DISCOVERED" /> 
     <action android:name="android.nfc.action.TAG_DISCOVERED" /> 
     <category android:name="android.intent.category.DEFAULT" /> 
    </intent-filter> 
    <meta-data 
     android:name="android.nfc.action.TECH_DISCOVERED" 
     android:resource="@xml/filter_nfc" /> 
</activity> 
</application> 

</manifest>  

코드

public class TagDiscoveredActivity extends Activity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.info); 
     etc 
    } 

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

private void resolveIntent(Intent intent) { 
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
      //| Intent.FLAG_ACTIVITY_SINGLE_TOP);   

    boolean handled = false; 

    // Parse the intent 
    final String action = intent.getAction(); 
    if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(action) || 
       NfcAdapter.ACTION_TAG_DISCOVERED.equals(action)) { 

     // When a tag is discovered we send it to the service to be save. We 
     // include a PendingIntent for the service to call back onto. This 
     // will cause this activity to be restarted with onNewIntent(). At 
     // that time we read it from the database and view it. 
     Parcelable nfctag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); 
     if (nfctag != null) { 
         //read tag and display here 
        } 
       } 
      } 

    if (!handled) { 
     Log.e(logTag, "Unknown intent " + intent); 
     finish(); 
     return; 
    } 
} 

내가 그것을 실행하고 두 번째 시나리오에 대한 로그 - 응용 프로그램 실행없이 NFC에서 직접 실행 - 로그 처음에는 작동하지만 두 번째에는 아무 것도 로깅하지 않습니다.

도움 주셔서 감사합니다.

답변

9

나는 모든 것을 시도한 후에 대답을 찾았습니다. , launchmode = "singleTask"와 onNewIntent의 코드에 줄을 추가합니다 :

대답은 안드로이드에 활동을 설정하는 것입니다

intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
startActivity(intent); 
+0

저장이, 감사를 내 인생 !! 나는 같은 의도가 Extras없이 다시 실행 된 유선 동작을 가졌습니다. 나는 왜이 의도가 어떻게 든 내부적으로 구원 받았다는 것을 모른다. – oli

+0

나에게도 일했습니다. 감사합니다! 그것은 안드로이드가되어야합니다 : launchMode 안드로이드 : launchmode ... – IAmCoder

1

아마도 당신이 찾고있는 것은 this example과 같은 전경 파견입니다. 나는 또한 재미있는 찾을 수있는 안드로이드 boilerplate (뻔뻔한 플러그)을 작성했습니다.

+1

먼저 링크가 죽었 ... – blackwolf

관련 문제