Android在NFC标签上多次启动活动 [英] Android launch activity multiple times on an NFC tag

查看:107
本文介绍了Android在NFC标签上多次启动活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Android应用有2个活动,一个主要用于信息活动,另一个用于接收NFC.

My Android app has 2 activities, a main one for info and one for receiving NFC.

首次启动该应用程序时,我可以多次读取NFC标签-每次都启动一个新活动并显示一些信息.

When launching the app for the first time, I can read NFC tags, multiple times - each time bringing up a new activity and showing some info.

如果应用已关闭,但手机已带至NFC标签-它将首次显示nfc标签活动,但再也不会响应其他标签了.

If the app is closed but the phone is brought to the NFC tag - it will show the nfc tag activity the first time, but never respond to any other tags again.

我在做什么错?!

第二个活动的清单部分和代码:

Manifest part and code for second activity:

<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.NFC" />
<uses-feature android:name="android.hardware.nfc" android:required="true" />

<application
android:icon="@drawable/aaa"
android:label="@string/app_name" 
    android:theme="@android:style/Theme.NoTitleBar">

<activity
    android:label="@string/app_name"
    android:name=".MainActivity">
    <intent-filter >
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

<activity 
    android:name=".TagDiscoveredActivity"
    android:screenOrientation="portrait">
    <intent-filter >
        <action android:name="android.nfc.action.TECH_DISCOVERED" />
        <action android:name="android.nfc.action.TAG_DISCOVERED" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
    <meta-data
        android:name="android.nfc.action.TECH_DISCOVERED"
        android:resource="@xml/filter_nfc" />
</activity>
</application>

</manifest>    

代码

public class TagDiscoveredActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.info);
        etc
    }

@Override
public void onNewIntent(Intent intent) {
    setIntent(intent);
    resolveIntent(intent);
}

private void resolveIntent(Intent intent) {
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            //| Intent.FLAG_ACTIVITY_SINGLE_TOP);        

    boolean handled = false;

    // Parse the intent
    final String action = intent.getAction();
    if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(action) ||
                NfcAdapter.ACTION_TAG_DISCOVERED.equals(action)) {

        // When a tag is discovered we send it to the service to be save. We
        // include a PendingIntent for the service to call back onto. This
        // will cause this activity to be restarted with onNewIntent(). At
        // that time we read it from the database and view it.
        Parcelable nfctag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
        if (nfctag != null) {
                        //read tag and display here
                    }
                }
            }

    if (!handled) {
        Log.e(logTag, "Unknown intent " + intent);
        finish();
        return;
    }
}

当我运行它并记录第二种情况时-在不运行应用程序的情况下直接从NFC启动-日志显示它是第一次运行,但是第二次,所有功能都没有记录任何东西.

When I run it and log for the second scenario - launching direct from NFC without the app running - the log shows it working first time, but the second time, none of the functions are logging anything.

感谢您提供任何有用的建议.

Thank you for any helpful suggestions.

推荐答案

尝试了一切之后,我终于找到了答案.

I found the answer finally after trying everything.

答案是将活动设置为android:launchmode ="singleTask", 并在onNewIntent的代码中添加以下行:

The answer is to set the activity to android:launchmode="singleTask", and in the code in onNewIntent add the lines:

intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

这篇关于Android在NFC标签上多次启动活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆