簡體   English   中英

防止在讀取NFC標簽時暫停應用程序

[英]Prevent application pausing while reading NFC tags

我正在構建一個Android應用程序,該應用程序將使用ndeftools庫讀取NFC標簽。

目前,當我在應用程序中讀取標簽時,它會最小化,並由內置的Android標簽閱讀器處理。 是否可以停止應用程序暫停並僅在TextView中顯示內容。

注意:TagReaderActivity只是擴展的NfcReaderActivity

這是我當前的代碼:

import org.ndeftools.Message;

import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.seaf.nfc.TagReaderActivity;

public class DemoNFCActivity extends TagReaderActivity {

    protected LinearLayout linearLayout;

    private static final String DEMO = DemoNFCActivity.class.getName();

    protected TextView feature;
    protected TextView state;
    protected TextView data;

    @Override
    public void onCreate(Bundle savedInstanceState) {


        this.setContentView(R.layout.layout_demo_nfc);

        feature = (TextView) findViewById(R.id.textView1);
        state = (TextView) findViewById(R.id.textView2);
        data = (TextView) findViewById(R.id.textView3);

        super.onCreate(savedInstanceState);

    }


    @Override
    protected void readNdefMessage(Message message) {

//      [INSERT SOLUTION HERE] :D
//      
//      if (data != null) {
//          
//          data.setText(message.toString());
//          data.setBackgroundColor(getResources().getColor(R.color.green_light));
//          
//      }

    }

    @Override
    protected void onNfcFeatureFound() {

        super.onNfcFeatureFound();

        if (feature != null) {

            feature.setText(getString(R.string.nfc_feature_true));
            feature.setBackgroundColor(getResources().getColor(R.color.green_light));

        }
    }

    @Override
    protected void onNfcFeatureNotFound() {

        super.onNfcStateDisabled();

        if (feature != null) {

            feature.setText(getString(R.string.nfc_feature_false));
            feature.setBackgroundColor(getResources().getColor(R.color.red_light));

        }

    }

    @Override
    protected void onNfcStateEnabled() {

        super.onNfcStateEnabled();

        if (state != null) {

            state.setText(getString(R.string.nfc_state_true));
            state.setBackgroundColor(getResources().getColor(R.color.green_light));

        }

    }

    @Override
    protected void onNfcStateDisabled() {

        super.onNfcStateDisabled();

        if (state != null) {

            state.setText(getString(R.string.nfc_state_false));
            state.setBackgroundColor(getResources().getColor(R.color.red_light));

        }

    }

}

NFC子系統首先調用onPause(),然后調用onNewIntent(),然后調用onResume()。 由於整個過程非常簡短,因此您無需解決此問題。

看來這里的問題是您的前台模式未激活。 請參見DefaultNfcReaderActivity中的onCreate()方法,並注意對

setDetecting(true);

之所以如此,是因為不必一定要始終接受傳入的NFC流量。

您需要注冊前台調度系統(如果您的應用程序僅是Android 4.4+,則需要注冊閱讀器模式系統)。 有關如何使用前台分派系統的更多信息,請參閱Android NFC 文檔

關於ndeftools庫, NfcDetectorActivity應該可以(或適應)您想要的內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM