2017-09-29 14 views
0

NFCカードから読み取るアプリケーションを開発しています。 問題は次のとおりです。 ユーザーがアプリをタップして起動したアプリケーションです。NFC Androidアプリケーションが2回起動される

<activity 
android:name="com.d_logic.cardcontrol.SplashScreenActivity" 
android:screenOrientation="portrait"> 
<intent-filter> 
    <action android:name="android.intent.action.MAIN"/> 
    <category android:name="android.intent.category.LAUNCHER"/> 
    <action android:name="android.nfc.action.TECH_DISCOVERED"/> 
</intent-filter> 
    <meta-data> 
    <android:name="android.nfc.action.TECH_DISCOVERED"> 
    <android:resource="@xml/techlist"/> 
</activity> 

アプリケーションが開始され、しばらく使用者プレスホームボタンの後:マニフェストに

//Initialize Foreground NFC Dispatch System 
mAdapter = NfcAdapter.getDefaultAdapter(this); 
context=this; 
mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, 
getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0); 
IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED); 
try { 
ndef.addDataType("*/*"); /* Handles all MIME based dispatches. 
          You should specify only the ones that you need. 
*/ 
} 
catch (IntentFilter.MalformedMimeTypeException e) { 
throw new RuntimeException("fail", e); 
} 
mFilters = new IntentFilter[] { ndef, }; 
mTechLists = new String[][] { new String[] { MifareClassic.class.getName() } 
}; 

は私が持っている: アプリケーションがフォアグラウンドディスパッチシステムを実装し、NFCカードをスキャンする準備ができています。 その後、バックグラウンドでアプリケーションを実行すると、ユーザーはアプリケーションを再起動しますが、今回はNFCカードを使用します。

この時点では、同じアプリケーションを実行している2つのインスタンスがあります。

アプリケーションの2回目の起動を防ぎますが、NFCカードでアプリケーションを起動できるようにするにはどうすればよいですか? ありがとう!二回、このロジックを使用して

答えて

0

避ける:

//put here in variable global 
private long twiceCall; 
private static final int time_interval = 1500; 

// and put this to avoiding twice calling 
if (twiceCall + time_interval > System.currentTimeMillis()) { 
     // If get the second time.. 
} else { 
     // Get the first time.. 
} 
twiceCall = System.currentTimeMillis(); 
0

活動の同じインスタンスがちょうど手前に持ってきて、新しいテントを渡されることを保証するために、マニフェストで

android:launchMode:"standard/singleTop/singleTask/singleInstance" 

を利用します。どのモードを選択するかは、正確なユースケースによって異なります。

"singleTop"または "singleTask"はNFCの使用例ではおそらく機能します。

関連する問題