简体   繁体   English

Android NFC重定向意图

[英]Android NFC Redirecting intent

When i put an nfc tag close to my mobile phone i want my app to start so i added this into my manifest: 当我将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" /> 

This works great. 这很好。

If a user is not logged in i want the user to log in first before he can transfer the data from the tag so i added this into my onCreate of my measure activity: 如果用户未登录,我希望用户先登录,然后才能从标记传输数据,因此我将其添加到我的度量活动的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();
}

But after the user is logged in he gets redirected to my main menu because of this implementation: 但是,由于此实现,用户登录后,他被重定向到我的主菜单:

/**
 * 
 * @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);
    }
}  

The 'MainActivity' mentioned here is my main menu. 这里提到的“ MainActivity”是我的主菜单。 but i want the activity to redirect to my measure activity but i don't know how i can do that. 但是我希望该活动重定向到我的测量活动,但是我不知道该怎么做。 after a "normal" login, without nfc intent, i need the app to redirect to the main menu but with nfc intent i want it to redirect to the measure activity. 在“正常”登录后,没有nfc意图,我需要将应用程序重定向到主菜单,但是具有nfc意图,我希望它重定向至测量活动。

Also when the user already is logged in i want the data of the tag to be transferred immediately. 另外,当用户已经登录时,我希望立即传输标签的数据。 now if i want to transfer the data i have to keep the tag close to the phone to start the measure activity and than to put it away and back again to transfer the data. 现在,如果我要传输数据,我必须将标签保持在手机附近以开始测量活动,而不是将其放回并再次返回以传输数据。

How can i do both things? 我该怎么办?

You are doing to many different things in the same code: reading nfc, accessing ui fields, controlling the flows of the activities. 您正在用同一代码处理许多不同的事情:读取nfc,访问ui字段,控制活动流。 (Yes, android sample code often give bad examples) (是的,Android示例代码通常会给出错误的示例)

You need to handle special cases like what is happening when the user just starts to login an then the tag is read. 您需要处理一些特殊情况,例如当用户刚开始登录然后读取标签时发生的情况。 Your only chance to get this right is to separate the different responsibilities in your code. 获得此权利的唯一机会是将代码中的不同职责分开。

One thing is sure: you need to store the information when a tag was read. 可以肯定的是:读取标签时,您需要存储信息。 So you can react differently and finally display it when the user has logged in. When there are different Activities involved, you probably need to store the data in some kind of central entity, like a singleton. 因此,您可以做出不同的反应,并最终在用户登录后显示它。当涉及到不同的活动时,您可能需要将数据存储在某种中央实体中,例如单例。 (Alternative is to always send the information together with the intent when you move from activity to another) (或者,当您从活动转移到另一个活动时,始终将信息与意图一起发送)

For NFC-reading, there is an example app "NFC-HUNT" from google which I found helpful. 对于NFC的阅读,我发现Google提供了一个示例应用程序“ NFC-HUNT”。 The has some special features: 具有一些特殊功能:

  1. it uses a special activity without UI that reads the NFC-Tag. 它使用没有UI的特殊活动来读取NFC标签。 This way NFC-reading is encapsulated, and the rest of your app has no NFC-Code at all. 这样,NFC读取就被封装了,您的应用程序的其余部分根本没有NFC代码。 This Activty (called NFCShimActivity) just ready the Infomation and then creates an Intent to start your normal UI. 此活动(称为NFCShimActivity)只是准备了信息,然后创建一个意图来启动您的常规UI。

  2. Normally, when an Activity is visible and new Intent for this Activity arrives (because NFC-Tag was read), another instance of the Actiity gets opened. 通常,当一个活动可见并且该活动的新意图到达时(因为已读取NFC标签),将打开该活动的另一个实例。 Just what you do not want. 正是您不想要的。

Solution is to set android:launchMode="singleTask" in Manifest, and to implement this method in the Activity: 解决方案是在清单中设置android:launchMode="singleTask" ,并在Activity中实现此方法:

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

I have written a blog entry about the nfc code reading structure, maybe it helps you to make it easier to understand 我写了一篇有关nfc代码读取结构的博客文章,也许它可以帮助您使其更易于理解

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM