2014-10-16 12 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/