Waiting for intent always makes new activity.

I am trying to make an application with nfc function. the problem is finding the nfc tag, waiting for the intent, always creating a new activity that already exists. I am using a tab. how to do pendingintent without taking a new action. Many thanks.

 public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);        

            mNfcAdapter = NfcAdapter.getDefaultAdapter(this);                              
            mNfcPendingIntent = PendingIntent.getActivity(this, 0,new Intent(this,
    getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
    }

    protected void onResume() {    
            super.onResume();
            mResumed = true;               
            // Sticky notes received from Android
            if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) {            
                NdefMessage[] messages = getNdefMessages(getIntent());
                byte[] payload = messages[0].getRecords()[0].getPayload();            
                try     { cekNfc(new String(payload)); }
                catch (SQLException e)          { e.printStackTrace(); } 
                catch (NoSuchAlgorithmException e)      {  e.printStackTrace(); }
                catch (UnsupportedEncodingException e)  { e.printStackTrace(); }

                setIntent(new Intent());
            }
            enableNdefExchangeMode();        
        }   
        private void enableNdefExchangeMode() { mNfcAdapter.enableForegroundDispatch(this, mNfcPendingIntent, mNdefExchangeFilters, null); } 

    NdefMessage[] getNdefMessages(Intent intent) {  // Parse the intent             
            NdefMessage[] msgs = null;        
            String action = intent.getAction();
            //jika ada action
            if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(action) || NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {                      
                Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
                if (rawMsgs != null) {
                    msgs = new NdefMessage[rawMsgs.length];
                    for (int i = 0; i < rawMsgs.length; i++) {  msgs[i] = (NdefMessage) rawMsgs[i]; }
                }
            } 
            return msgs;
        }


 public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);        

            mNfcAdapter = NfcAdapter.getDefaultAdapter(this);                              
            mNfcPendingIntent = PendingIntent.getActivity(this, 0,new Intent(this,
    getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
    }

    protected void onResume() {    
            super.onResume();
            mResumed = true;               
            // Sticky notes received from Android
            if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) {            
                NdefMessage[] messages = getNdefMessages(getIntent());
                byte[] payload = messages[0].getRecords()[0].getPayload();            
                try     { cekNfc(new String(payload)); }
                catch (SQLException e)          { e.printStackTrace(); } 
                catch (NoSuchAlgorithmException e)      {  e.printStackTrace(); }
                catch (UnsupportedEncodingException e)  { e.printStackTrace(); }

                setIntent(new Intent());
            }
            enableNdefExchangeMode();        
        }   
        private void enableNdefExchangeMode() { mNfcAdapter.enableForegroundDispatch(this, mNfcPendingIntent, mNdefExchangeFilters, null); } 

    NdefMessage[] getNdefMessages(Intent intent) {  // Parse the intent             
            NdefMessage[] msgs = null;        
            String action = intent.getAction();
            //jika ada action
            if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(action) || NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {                      
                Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
                if (rawMsgs != null) {
                    msgs = new NdefMessage[rawMsgs.length];
                    for (int i = 0; i < rawMsgs.length; i++) {  msgs[i] = (NdefMessage) rawMsgs[i]; }
                }
            } 
            return msgs;
        }
+5
source share
6 answers

Place android:launchMode="singleTask"for your activity (or actions) in the manifest. It does the trick. Whenever an NFC system is sent by the system, a new action is always created. This is unique to NFC intentions. Thus, the setup android:launchMode="singleTop"will not work and will not set flags in the PendingIntent.

NfcAdapter.enableForegroundDispatch() . , NFC ( onNewIntent()).

+7

:

mNotificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

:

android:launchMode="singleTask"
+2

, :

PendingIntent.getActivity(this, 0,new Intent(this,
    getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP
    |Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT
    |Intent.FLAG_ACTIVITY_REORDER_TO_FRONT), 0);
0

"PendingIntent.FLAG_UPDATE_CURRENT" getActivity. I2m, .

0

.

new Intent(this,
    getClass()).addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY)
0

I had a similar problem, but not using TabHost - every time the NFC tag was checked, my application started a new activity, and did not shoot onNewIntentas I wanted. I tried to set android:launchMode="singleTask"in the manifest and use NfcAdapter.enableForegroundDispatch()in my activity onResume. Instead, I refused to use PendingIntent and set the intent to filter my activity in the manifest as follows:

    <activity
        ...
        android:launchMode="singleTask">
        <intent-filter>
            <action android:name="android.nfc.action.NDEF_DISCOVERED" />
            <data android:mimeType="application/vnd.myname.myapp" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.nfc.action.TECH_DISCOVERED" />
        </intent-filter>
        <meta-data
            android:name="android.nfc.action.TECH_DISCOVERED"
            android:resource="@xml/nfc_tech_filter" />
    </activity>
0
source

All Articles