Android的NFC读取标签的问题。活动开始的数据,每次收到 [英] Android NFC read Tags issue. Activity starts each time on data received

查看:798
本文介绍了Android的NFC读取标签的问题。活动开始的数据,每次收到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个问题,阅读NFC标签。

首先是标签读取活动创建时接收到的标签各一次。

和第二个问题是活动在全屏幕窗口中打开,而不是在选项卡主机活动,但第一个问题是最糟糕的。

我该怎么办(AndroidManifest.xml中):

 <活动
    机器人:名称=readingActivity>
    <意向滤光器>
        <作用机器人:名称=android.nfc.action.TAG_DISCOVERED/>

        <类机器人:名称=android.intent.category.DEFAULT/>
    &所述; /意图滤光器>
< /活性GT;
 

和readingActivity.cs:

  @覆盖
    公共无效的onCreate(包savedInstanceState){
        Log.d(W,的onCreate);
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.readingActivity);
    }

    @覆盖
    公共无效onResume(){
        super.onResume();
        Log.d(W,onResume);

        PendingIntent意图= PendingIntent.getActivity(此,0,getIntent(),0);
         NfcAdapter.getDefaultAdapter(本).enableForegroundDispatch(这一点,意图,
                  NULL,NULL);
    }

    @覆盖
    保护无效的onPause(){
        super.onPause();
        如果(NfcAdapter.getDefaultAdapter(这一点)!= NULL)
        NfcAdapter.getDefaultAdapter(本).disableForegroundDispatch(本);
    }
 

日志:

  02-28 18:22:19.949:D / W(4513):的onCreate

02-28 18:22:19.949:D / W(4513):onResume

02-28 18:22:21.078:D / W(4513):的onCreate

02-28 18:22:21.082:D / W(4513):onResume
 

解决方案

现在的问题是,在 PendingIntent 。该 getIntent()检索意图的开始你的活动 ,所以它传递给 PendingIntent 将导致启动它另一个时间。

而不是 getIntent()使用类似新意图(this.getApplicationContext(),this.getClass())

I have 2 issues with reading NFC Tags.

First is Tag Read Activity creates each time when tag received.

And second issue is activity opens in full screen window, not under Tab Host Activity, but first issue is worst.

What do I do ( AndroidManifest.xml ):

<activity
    android:name="readingActivity" >
    <intent-filter>
        <action android:name="android.nfc.action.TAG_DISCOVERED" />

        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>

and readingActivity.cs:

    @Override
    public void onCreate(Bundle savedInstanceState) {
        Log.d("W", "onCreate");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.readingActivity);
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.d("W", "onResume");

        PendingIntent intent = PendingIntent.getActivity(this, 0, getIntent(), 0);
         NfcAdapter.getDefaultAdapter(this).enableForegroundDispatch(this, intent, 
                  null, null);
    }

    @Override
    protected void onPause() {
        super.onPause();
        if(NfcAdapter.getDefaultAdapter(this) != null)
        NfcAdapter.getDefaultAdapter(this).disableForegroundDispatch(this);
    }

Logs:

02-28 18:22:19.949: D/W(4513): onCreate

02-28 18:22:19.949: D/W(4513): onResume

02-28 18:22:21.078: D/W(4513): onCreate

02-28 18:22:21.082: D/W(4513): onResume

解决方案

The problem is in the PendingIntent. The getIntent() retrieves the Intent that started your Activity, so passing it to the PendingIntent will result in starting it another time.

Instead of getIntent() use something like new Intent(this.getApplicationContext(), this.getClass()).

这篇关于Android的NFC读取标签的问题。活动开始的数据,每次收到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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