2015-09-17 37 views
3

我試圖在對NFC標籤的寫入操作完成後開始新的activity。我試圖用一個處理程序,但它不工作時,標籤被writen成功,但處理器犯規啓動activity,它應該寫操作寫入NFC標籤後開始新活動

private void formatTag(Tag tag, NdefMessage ndefMessage) 
    { 
     NdefFormatable ndefFormatable = NdefFormatable.get(tag); 

     if (ndefFormatable == null) 
     { 
      Toast.makeText(this, "Tag is not NDEF formatable", Toast.LENGTH_LONG).show(); 
      return; 
     } 

     try 
     { 
      ndefFormatable.connect(); 
      ndefFormatable.format(ndefMessage); 
      ndefFormatable.close(); 
      Toast.makeText(this, "Tag has be written successfully!", Toast.LENGTH_LONG).show(); 
      writeHandler.sendEmptyMessage(0); 

     } 
     catch (Exception e) 
     { 
      Log.e("formatTag: ", e.getMessage()); 
     } 


    } 

    private Handler writeHandler = new Handler() { 
     public void handleMessage(Message msg) { 
      Intent nextActivity = new Intent(WriteCardActivity.this, MainActivity.class); 
      startActivityForResult(nextActivity, 0); 
      WriteCardActivity.this.finish(); 
     } 
    }; 

這裏之後推出的清單

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.loyalty.cardplanet.membershipcard" > 

    <uses-permission android:name="android.permission.NFC" /> 
    <uses-permission android:name="android.permission.INTERNET"/> 

    <uses-feature 
     android:name="android.hardware.nfc" 
     android:required="false" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name=".MainActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity 
      android:name=".RegisterActivity" 
      android:label="@string/title_activity_register" > 
     </activity> 
     <activity 
      android:name=".RedeemActivity" 
      android:label="@string/title_activity_redeem" > 
     </activity> 
     <activity 
      android:name=".PurchaseActivity" 
      android:label="@string/title_activity_purchase" > 
     </activity> 
     <activity 
      android:name=".ResetPinActivity" 
      android:label="@string/title_activity_reset_pin" > 
     </activity> 
     <activity 
      android:name=".WriteCardActivity" 
      android:label="@string/title_activity_write_card" > 
     </activity> 
    </application> 

</manifest> 
+0

您是否有任何日誌?請在此處添加一個清單 – Gaskoin

+0

我已添加清單 –

+0

嘗試在UI上運行線程 – ThomasRS

回答

3

我想通了。我決定使用AlertDialog代替Handler所以我刪除了Handler部分並添加這onNewIntent

@Override 
    protected void onNewIntent(Intent intent) { 


     Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); 

     NdefMessage ndefMessage = createNdefMessage(account+""); 

     writeNdefMessage(tag, ndefMessage); 

     AlertDialog.Builder alertDialog = new AlertDialog.Builder(WriteCardActivity.this); 
     alertDialog.setMessage("Card Written Successfully!"); 

     alertDialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int id) { 
       Intent intent = new Intent(WriteCardActivity.this, MainActivity.class); 
       Bundle b = new Bundle(); 
       b.putBoolean("new_window", true); //sets new window 
       intent.putExtras(b); 
       startActivity(intent); 
      } 
     }); 


     alertDialog.create(); 
     alertDialog.show(); 

     super.onNewIntent(intent); 
    } 
3

您的MainActivity上有IntentFilter,因此Intent應與活動的IntentFilter匹配。

所以,你應該開始你的活動是這樣的:

Intent nextActivity = new Intent(WriteCardActivity.this, MainActivity.class); 
nextActivity.setAction(Intent.ACTION_MAIN); 
nextActivity.addCategory(Intent.CATEGORY_LAUNCHER); 

startActivity(nextActivity); 
WriteCardActivity.this.finish(); 
+0

我不明白。你建議我做什麼? –

+0

我已更新我的帖子。另請閱讀關於意圖和意圖過濾的內容http://developer.android.com/guide/components/intents-filters.html – Gaskoin

+0

它仍然不起作用 –

1

此代碼爲我工作。並且不要從MainActivity的onCreate,onResume或onPause調用WriteCardActivity。否則WriteCardActivity將再次開始。

private void formatTag(Tag tag, NdefMessage ndefMessage) 
{ 
    NdefFormatable ndefFormatable = NdefFormatable.get(tag); 

    if (ndefFormatable == null) 
    { 
     Toast.makeText(this, "Tag is not NDEF formatable", Toast.LENGTH_LONG).show(); 
     return; 
    } 

    try 
    { 
     ndefFormatable.connect(); 
     ndefFormatable.format(ndefMessage); 
     ndefFormatable.close(); 
     Toast.makeText(this, "Tag has be written successfully!", Toast.LENGTH_LONG).show(); 

     Intent nextActivity = new Intent(WriteCardActivity.this, MainActivity.class); 
     startActivityForResult(nextActivity, 0); 
     WriteCardActivity.this.finish(); 

    } 
    catch (Exception e) 
    { 
     Log.e("formatTag: ", e.getMessage()); 
    } 


}