Android的NFC读取标签的问题。活动开始的数据,每次收到标签、数据、问题、Android

由网友(思念的钟、只为你跳动)分享简介:我有2个问题,阅读NFC标签。首先是标签读取活动创建时接收到的标签各一次。和第二个问题是活动在全屏幕窗口中打开,而不是在选项卡主机活动,但第一个问题是最糟糕的。我该怎么办(AndroidManifest.xml中):<活动机器人:名称=readingActivity><意向滤光器><作用机...

我有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(本);
    }
 
Android中读取NFC标签卡中的ID

日志:

  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()).