Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
540 views
in Technique[技术] by (71.8m points)

java - Pending intent always make new activity

i trying to make app with nfc function. the problem is when nfc tag discovered, pending intent always make a new activity that already exist. i'm using tab host. how to make pendingintent without making a new activity. thanks a lot.

 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;
        }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Put android:launchMode="singleTask" for your activity (or activities) in the manifest. That does the trick. Whenever an NFC intent is dispatched by the system, always a new Activity will be created. This is unique for NFC intents. So setting android:launchMode="singleTop" will not work, nor will setting flags in the PendingIntent.

Another solution is to use NfcAdapter.enableForegroundDispatch() in all your Activities. That way your app gets to handle all NFC intents itself directly (via onNewIntent()).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

Just Browsing Browsing

[1] html - How to create even cell spacing within a

2.1m questions

2.1m answers

60 comments

56.8k users

...