2014-10-16 48 views
0

當我把貼近我的手機的NFC標籤,我想我的應用程序啓動 所以我說這到我的清單:Android的NFC重定向意圖

<intent-filter> 
    <action android:name="android.nfc.action.TECH_DISCOVERED" /> 
    <category android:name="android.intent.category.DEFAULT" /> 
</intent-filter> 
<meta-data android:name="android.nfc.action.TECH_DISCOVERED" android:resource="@xml/nfc_tech_filter" /> 

這個偉大的工程。

如果用戶沒有登錄我希望用戶先登錄 ,纔可以從標籤,所以我加入到我的 的onCreate這是我衡量活動進行傳送的數據:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    this.sessionManager = new SessionManager(this); 
    this.sessionManager.login(); 

    this.setContentView(R.layout.activity_measure); 
    this.nfcAdapter = NfcAdapter.getDefaultAdapter(this); 
    this.pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0); 

    IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED); 

    this.filters = new IntentFilter[] { ndef, }; 
    this.techLists = new String[][] { new String[] { android.nfc.tech.NfcV.class.getName() } }; 

    this.textViewYear = (TextView) findViewById(R.id.measure_textview_year); 
    this.textViewMonthDay = (TextView) findViewById(R.id.measure_textview_month_day); 
    this.textViewTime = (TextView) findViewById(R.id.measure_textview_time); 
    this.textViewGlucose = (TextView) findViewById(R.id.measure_textview_glucose); 

    new StartReadTask().execute(); 
} 

但用戶登錄後,他被重定向到我的主菜單,因爲這個 實現的:

/** 
* 
* @param v 
*/ 
@Override 
public void onClick(View view) { 
    String username = this.textEditUsername.getText().toString(); 
    String password = this.textEditPassword.getText().toString(); 

    if(username.trim().length() > 0 && password.trim().length() > 0){ 
     if(username.equals("test") && password.equals("test")){ 
      this.sessionManager.createLoginSession("Test-Name", "Test-Email"); 

      Intent intent = new Intent(this.getApplicationContext(), MainActivity.class); 

      this.startActivity(intent); 
      this.finish(); 
     } else { 
      this.dialogAlertManager.showAlertDialog(LoginActivity.this, "Login failed..", "Username or Password is incorrect", false); 
     }    
    } else { 
     this.dialogAlertManager.showAlertDialog(LoginActivity.this, "Login failed..", "Please enter username and password", false); 
    } 
} 

這裏所說的「MainActivity」是我的主菜單。但我想活動 重定向到我的測量活動,但我不知道我該怎麼做。 「正常」登錄後,沒有nfc意圖,我需要應用程序重定向到主菜單 但與nfc意圖我希望它重定向到度量活動。

此外,當用戶已經被記錄在我想要的標籤的數據將被立即傳送 。現在,如果我想要傳輸數據,我必須保持標記靠近 電話才能開始測量活動,然後再放回去,然後再回到 傳輸數據。

我該怎麼做這兩件事?

回答

0

你正在做許多不同的事情在同一代碼:讀取NFC,訪問用戶界面領域,控制活動的流程。 (是的,Android的代碼示例往往會給壞的例子)

您需要處理的特殊情況下,像正在發生的事情,當用戶剛開始登錄的,則標籤被讀取。你唯一的機會就是把代碼中的不同職責分開。

有一件事是肯定的:你需要存儲的信息時,讀取​​標籤。所以你可以做出不同的反應,並最終在用戶登錄時顯示它。當涉及不同的活動時,你可能需要將數據存儲在某種中央實體中,例如單例。 (另一種方法是始終與意圖一起發送的信息,當您從一個活動移動到另一個)

對於NFC閱讀,還有一個示例應用程序「NFC-HUNT」從谷歌,我發現有幫助。 將有一些特殊的功能:

  1. 它使用了一個特殊的活動,而用戶界面,讀取NFC的標籤。這種方式可以封裝NFC閱讀,而其他應用根本沒有NFC-Code。這個活動(稱爲NFCShimActivity)剛剛準備好Infomation,然後創建一個Intent來啓動你的普通用戶界面。

  2. 通常情況下,當一個活動是可見的,新的意向此活動的到來(因爲NFC的標籤被讀取),該活性研究的另一個實例被打開。就是你不想要的。

解決辦法是設置在清單android:launchMode="singleTask",並落實在活動這個方法:

public void onNewIntent(Intent intent) {...} 

我曾寫過關於NFC讀碼結構的博客條目,也許它可以幫助你做出它更容易理解

http://meier-online.com/en/2014/09/nfc-hunt-analysed/