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
170 views
in Technique[技术] by (71.8m points)

android - NFC Tag Reading

I got an application that reads and writes nfc tags with ndef data format. This all seems to be ok. But whenever i try to get closer a tag to my phone, it produces a new activity. I only want to obtain data on the tag without opening new intent. So i can make my app decide whether the tag has been tapped two times in a row.

The code that i handle coming tags:

public class MainActivity extends Activity {

public static Context myContext;
private Button myButton;
private int currentID, currentBalance;

protected void onCreate(Bundle savedInstanceState) { // Firstly Created when
                                                        // a tag tapped to
                                                        // the phone
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    myButton = (Button) findViewById(R.id.button1);

    currentID = 0;
    currentBalance = 0;


    myButton.setOnClickListener(new View.OnClickListener() { // Tag write
                                                                // function
                                                                // places
                                                               // here

        public void onClick(View arg0) {

            Intent i = new Intent(getApplicationContext(), nfcWrite.class);
            i.putExtra("id",currentID);
            i.putExtra("balance",currentBalance);
            startActivity(i);

        }
    });

}



@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();


    Intent intent = getIntent();

    if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) {
        Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
        NdefRecord myRecord = ((NdefMessage) rawMsgs[0]).getRecords()[0];
        String nfcData = new String(myRecord.getPayload());
        String[] splitData = nfcData.split("-");
        currentID = Integer.parseInt(splitData[0]);
        currentBalance = Integer.parseInt(splitData[1]);

        Toast.makeText(getApplicationContext(), nfcData, Toast.LENGTH_LONG).show();
    }


}

}
See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

Add android:launchMode="singleTask" to the <activity> element for MainActivity. Note that if the activity is already running, onNewIntent() will be called, instead of onCreate(), to deliver the NDEF Intent to you. Hence, you will need to handle the NDEF Intent in both onCreate() (if the activity was not already running) and onNewIntent() (if the activity was already running).

This sample project illustrates the technique.


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

...