2015-11-03 31 views
1

我正在开发一款应检测NFC标签的应用程序。 我的问题是,我的活动每次应用程序扫描标签时重新打开。它应该只在应用程序关闭时打开。但是当它处于活动状态时,我只需要标签中的数据。当发现NFC标签时活动重新开启

清单:

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

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

<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme" > 
    <activity 
     android:name=".MainActivity" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme.NoActionBar" 
     > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
     <intent-filter> 
      <action android:name="android.nfc.action.NDEF_DISCOVERED"/> 
      <category android:name="android.intent.category.DEFAULT"/> 
      <data android:mimeType="text/plain" /> 
     </intent-filter> 
    </activity> 
</application> 

</manifest> 

活动的onCreate & handleIntent

rotected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.activity_main); 

    //Fragment ReadOrWrite 
    fRead = (Fragment_Read) Fragment.instantiate(getApplicationContext(), Fragment_Read.class.getName(), null); 
    Fragment_ReadOrWrite fReadOrWrite = (Fragment_ReadOrWrite) Fragment.instantiate(getApplicationContext(), Fragment_ReadOrWrite.class.getName(), null); 
    fReadOrWrite.setFragment_Read(fRead); 
    FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction(); 
    fragmentTransaction.replace(R.id.id_activity_main, fReadOrWrite); 
    fragmentTransaction.commit(); 


    //NFC 

    mNfcAdapter = NfcAdapter.getDefaultAdapter(this); 

    handleIntent(getIntent()); 
} 

private void handleIntent(Intent intent) { 
    String action = intent.getAction(); 
    if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) { 

     String type = intent.getType(); 
     if (MIME_TEXT_PLAIN.equals(type)) { 

      Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); 
      NdefReaderTask ndefReader = new NdefReaderTask(fRead); 
      ndefReader.execute(tag); 

     } else { 
      System.out.println("Wrong mime type: " + type); 
     } 
    } 
} 
+1

现在有效。这是有帮助的:http://thehelpcentre.xyz/question/28063480/can-i-read-nfc-tag-in-activity-without-restart-it 我增加了'disableForegroundDispatch'和'enableForegroundDispatch'。 –

+0

[Android的可能重复:如何禁用扫描NFC标签时重新打开应用程序?](http://stackoverflow.com/questions/28496577/android-how-do-you-disable-that-your-app -re-ne-scanning-when-scanning-a-nfc-tag) –

回答

1

在主要活动清单只需使用launchMode="singleTop"。 这将确保如果您的活动位于任务堆栈的顶部,则不会被重新创建。

请注意,在这种情况下onCreate不再被调用,所以如果要从意向读取内容,则需要覆盖onNewIntent Activity方法。

+0

感谢您的回答! 不幸的是,它不起作用。我的活动仍然重新加载,并且不会调用onNewIntent方法。 –

相关问题