Android NFC read Tags issue. Activity starts each time on data received

流过昼夜 提交于 2019-12-11 03:52:23

问题


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

回答1:


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



来源:https://stackoverflow.com/questions/9485819/android-nfc-read-tags-issue-activity-starts-each-time-on-data-received

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!