2016-01-20 55 views
0

我想将resolveIntent()函数的nfcID传递给下一个intent或接口。我试过把下面的代码放入函数中如何将NFC tagInfo从一个意图转移到另一个意向(一个界面到另一个界面)?

Intent i = new Intent(NFCLogin.this, User.class); 
i.putExtra("nfcID",nfcID); 
startActivity(i); 

但是应用程序然后崩溃了。请帮帮我。

package com.example.johnJones.passkiller; 

import android.content.Intent; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.app.PendingIntent; 
import android.content.IntentFilter; 
import android.nfc.NfcAdapter; 
import android.nfc.tech.IsoDep; 
import android.nfc.tech.MifareClassic; 
import android.nfc.tech.MifareUltralight; 
import android.nfc.tech.Ndef; 
import android.nfc.tech.NfcA; 
import android.nfc.tech.NfcB; 
import android.nfc.tech.NfcF; 
import android.nfc.tech.NfcV; 
import android.widget.Toast; 
import android.widget.TextView; 

public class NFCLogin extends AppCompatActivity { 

private final String[][] techList = new String[][] { 
     new String[] { 
       NfcA.class.getName(), 
       NfcB.class.getName(), 
       NfcF.class.getName(), 
       NfcV.class.getName(), 
       IsoDep.class.getName(), 
       MifareClassic.class.getName(), 
       MifareUltralight.class.getName(), Ndef.class.getName() 
     } 
}; 

String nfcID = ""; 
NfcAdapter mAdapter; 
//DatabaseHelper helper = new DatabaseHelper(this); 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_nfclogin); 

} 

@Override 
protected void onResume() { 
    super.onResume(); 
    // creating pending intent: 
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0); 
    // creating intent receiver for NFC events: 
    IntentFilter filter = new IntentFilter(); 
    filter.addAction(NfcAdapter.ACTION_TAG_DISCOVERED); 
    filter.addAction(NfcAdapter.ACTION_NDEF_DISCOVERED); 
    filter.addAction(NfcAdapter.ACTION_TECH_DISCOVERED); 
    // enabling foreground dispatch for getting intent from NFC event: 
    NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this); 
    nfcAdapter.enableForegroundDispatch(this, pendingIntent, new IntentFilter[]{filter}, this.techList); 
} 

@Override 
protected void onPause() { 
    super.onPause(); 
    // disabling foreground dispatch: 
    NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this); 
    nfcAdapter.disableForegroundDispatch(this); 
} 

@Override 
protected void onNewIntent(Intent intent) { 
    super.onNewIntent(intent); 
    resolveIntent(intent); 
} 

private void resolveIntent(Intent intent){ 
    String action = intent.getAction(); 
    if (action.equals(NfcAdapter.ACTION_TAG_DISCOVERED)) { 
     nfcID = ByteArrayToHexString(intent.getByteArrayExtra(NfcAdapter.EXTRA_ID)); 
     Toast.makeText(NFCLogin.this, nfcID, Toast.LENGTH_SHORT).show(); 

    } 
} 


private String ByteArrayToHexString(byte [] inarray) { 
    int i, j, in; 
    String [] hex = {"0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"}; 
    String out= ""; 

    for(j = 0 ; j < inarray.length ; ++j) 
    { 
     in = (int) inarray[j] & 0xff; 
     i = (in >> 4) & 0x0f; 
     out += hex[i]; 
     i = in & 0x0f; 
     out += hex[i]; 
    } 
    return out; 
} 
} 

我想将nfcID传递到User.class的下一个接口,但应用程序崩溃。

+0

请把logcat的崩溃 – LaurentY

回答

0

这是我通过一个NFC意向到另一个活动(这里命名ResultActivity):

Intent mainintent = getIntent(); if (mainintent.getAction().equals(NfcAdapter.ACTION_NDEF_DISCOVERED)) { //transfer the NDEF data to a new intent Parcelable[] rawMsgs = mainintent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES); Intent myintent = new Intent(this, ResultActivity.class); myintent.setAction(NfcAdapter.ACTION_NDEF_DISCOVERED); myintent.putExtra(NfcAdapter.EXTRA_NDEF_MESSAGES, rawMsgs); myintent.putExtra(NfcAdapter.EXTRA_TAG, mainintent.getParcelableExtra(NfcAdapter.EXTRA_TAG)); startActivity(myintent); }

相关问题