2015-07-17 6 views
0

Я пытаюсь записать сообщение NDEF в тег NFC. У меня есть код для написания сообщения NDEF в onNewIntent(). Но элемент управления не будет включен вNewIntent(). После onResume() он висит. PLS найти ниже кода.Проблема в письменной форме Сообщение NDEF для тега NFC

public class MainActivity extends Activity { 
    private BluetoothAdapter mBluetoothAdapter; 
    private NfcAdapter mNfcAdapter; 
    static final byte[] TYPE_BT_OOB = "application/vnd.bluetooth.ep.oob". 
      getBytes(Charset.forName("US_ASCII")); 
    private NdefMessage mNdefMessage; 
    String mLocalBluetoothAddress; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Toast.makeText(this, "inside oncreate", Toast.LENGTH_LONG).show(); 

     mNfcAdapter = NfcAdapter.getDefaultAdapter(this); 
     if (mNfcAdapter == null) { 
      // Stop here, we definitely need NFC 
      Toast.makeText(this, "This device doesn't support NFC.", Toast.LENGTH_LONG).show(); 
      finish(); 
      return; 

     } 
    } 
    public void onResume() { 
     super.onResume(); 
     Toast.makeText(this, "inside OnResume", Toast.LENGTH_LONG).show(); 
     NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this); 
     PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0); 
     nfcAdapter.enableForegroundDispatch(this, pendingIntent, null, null); 
    } 

    public void onNewIntent(Intent intent) { 
     Toast.makeText(this, "inside onnewintent", Toast.LENGTH_LONG).show(); 

     if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) { 
      Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); 

      if (tag != null) { 
       Ndef ndef = Ndef.get(tag); 
       if (ndef != null) { 
        try { 
         ndef.connect(); 
         ndef.writeNdefMessage(createHandoverRequestMessage()); 
        } catch (Exception e) { 
         Log.e("TagWriting", e.toString()); 
        } finally { 
         try { 
          ndef.close(); 
         } catch (Exception e) { 
         } 
        } 
       } 
      } 

     } 

    } 
    public void onPause() { 
     super.onPause(); 
     NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this); 
     nfcAdapter.disableForegroundDispatch(this); 
    } 

Я ценю любую помощь, чтобы получить эту информацию.

ответ

0

Проверьте файл манифеста, чтобы увидеть

<activity 
    android:name=".MainActivity" 
    android:alwaysRetainTaskState="true" 
    android:configChanges="orientation|keyboardHidden|screenSize" 
    android:label="@string/app_name" 
    android:launchMode="singleInstance" 
    android:screenOrientation="nosensor" > 

андроид: alwaysRetainTaskState = «истинный»

(я использую следующий метод, и он работает) Если это нормально, то попробуйте поймать Pending намерение onCreate().

public class MainActivity extends Activity { 
    private BluetoothAdapter mBluetoothAdapter; 
    private NfcAdapter mNfcAdapter; 
    static final byte[] TYPE_BT_OOB = "application/vnd.bluetooth.ep.oob". 
      getBytes(Charset.forName("US_ASCII")); 
    private NdefMessage mNdefMessage; 

    private PendingIntent pendingIntent; 
    String mLocalBluetoothAddress; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Toast.makeText(this, "inside oncreate", Toast.LENGTH_LONG).show(); 

     mNfcAdapter = NfcAdapter.getDefaultAdapter(this); 
     if (mNfcAdapter == null) { 
      // Stop here, we definitely need NFC 
      Toast.makeText(this, "This device doesn't support NFC.", Toast.LENGTH_LONG).show(); 
      finish(); 
      return; 

     } else { 
      pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0); 
      onTagFound(getIntent()); 
     } 
    } 

    public void onResume() { 
     super.onResume(); 
     Toast.makeText(this, "inside OnResume", Toast.LENGTH_LONG).show(); 
     NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this); 
     if (nfcAdapter != null && pendingIntent != null) { 
      nfcAdapter.enableForegroundDispatch(this, pendingIntent, null, null); 
     } 
    } 

    public void onNewIntent(Intent intent) { 
     Toast.makeText(this, "inside onnewintent", Toast.LENGTH_LONG).show(); 
     onTagFound(intent); 
    } 

    public void onTagFound(Intent intent) { 
     if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) { 
      Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); 
      if (tag != null) { 
       Ndef ndef = Ndef.get(tag); 
       if (ndef != null) { 
        try { 
         ndef.connect(); 
         ndef.writeNdefMessage(createHandoverRequestMessage()); 
        } catch (Exception e) { 
         Log.e("TagWriting", e.toString()); 
        } finally { 
         try { 
          ndef.close(); 
         } catch (Exception e) { 
         } 
        } 
       } 
      } 

     } 
    } 

    public void onPause() { 
     super.onPause(); 
     NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this); 
     nfcAdapter.disableForegroundDispatch(this); 
    } 
} 
Смежные вопросы