[英]Android NFC Redirecting intent
當我將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意圖,我希望它重定向至測量活動。
另外,當用戶已經登錄時,我希望立即傳輸標簽的數據。 現在,如果我要傳輸數據,我必須將標簽保持在手機附近以開始測量活動,而不是將其放回並再次返回以傳輸數據。
我該怎么辦?
您正在用同一代碼處理許多不同的事情:讀取nfc,訪問ui字段,控制活動流。 (是的,Android示例代碼通常會給出錯誤的示例)
您需要處理一些特殊情況,例如當用戶剛開始登錄然后讀取標簽時發生的情況。 獲得此權利的唯一機會是將代碼中的不同職責分開。
可以肯定的是:讀取標簽時,您需要存儲信息。 因此,您可以做出不同的反應,並最終在用戶登錄后顯示它。當涉及到不同的活動時,您可能需要將數據存儲在某種中央實體中,例如單例。 (或者,當您從活動轉移到另一個活動時,始終將信息與意圖一起發送)
對於NFC的閱讀,我發現Google提供了一個示例應用程序“ NFC-HUNT”。 具有一些特殊功能:
它使用沒有UI的特殊活動來讀取NFC標簽。 這樣,NFC讀取就被封裝了,您的應用程序的其余部分根本沒有NFC代碼。 此活動(稱為NFCShimActivity)只是准備了信息,然后創建一個意圖來啟動您的常規UI。
通常,當一個活動可見並且該活動的新意圖到達時(因為已讀取NFC標簽),將打開該活動的另一個實例。 正是您不想要的。
解決方案是在清單中設置android:launchMode="singleTask"
,並在Activity中實現此方法:
public void onNewIntent(Intent intent) {...}
我寫了一篇有關nfc代碼讀取結構的博客文章,也許它可以幫助您使其更易於理解
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.