第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

當(dāng)應(yīng)用程序關(guān)閉時,Android NFC 總是調(diào)用 onCreate

當(dāng)應(yīng)用程序關(guān)閉時,Android NFC 總是調(diào)用 onCreate

動漫人物 2023-06-21 13:42:04
我制作了檢測 nfc 標(biāo)簽的應(yīng)用程序。一切正常,當(dāng)我的應(yīng)用程序關(guān)閉并且我用手機(jī)掃描 nfc 標(biāo)簽時,它向我顯示一個具有 onCreate() 方法的活動,當(dāng)我再次掃描它的第二次工作時,我不知道我的生命周期是否錯誤應(yīng)用程序或者我在代碼中遺漏了什么?當(dāng)我打開應(yīng)用程序時,掃描正在工作:應(yīng)用程序關(guān)閉時的第一張照片第二張照片:來自第二張照片,但在第二次掃描中它有效這是我的代碼public class NfcActivity extends AppCompatActivity {    private static final String TAG = "NfcActivity";    private NfcAdapter mNfcAdapter;    private TextView mTextView;    PendingIntent pendingIntent;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_nfc);        mTextView = findViewById(R.id.tv_nfc_detail);        mNfcAdapter = NfcAdapter.getDefaultAdapter(getApplicationContext());        if (mNfcAdapter == null) {            Toast.makeText(this, "Cet appareil ne supporte pas nfc", Toast.LENGTH_SHORT).show();            finish();            return;        }        if (!mNfcAdapter.isEnabled()) {            startActivity(new Intent("android.settings.NFC_SETTINGS"));            Toast.makeText(this, "Activer nfc", Toast.LENGTH_SHORT).show();        }        pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this,            getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);    }    @Override    protected void onPause() {        super.onPause();        mNfcAdapter.disableForegroundDispatch(this);    }    @Override    protected void onResume() {        super.onResume();        pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this,            getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);        IntentFilter[] intentFilters = new IntentFilter[]{};        mNfcAdapter.enableForegroundDispatch(this, pendingIntent, intentFilters, null);    }
查看完整描述

1 回答

?
翻過高山走不出你

TA貢獻(xiàn)1875條經(jīng)驗(yàn) 獲得超3個贊

您只是在第二次處理意圖。

添加一個基于您當(dāng)前onNewIntent()方法的新方法,如下所示:

private void onNewNfcTag(Intent intent) {

? ? if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())

? ? ? ? ? ? || NfcAdapter.ACTION_TECH_DISCOVERED.equals(intent.getAction())?

? ? ? ? ? ? || NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) {

? ? ? ? Tag iTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);

? ? ? ? mTextView.setText(TagReader.readTag(iTag, intent));

? ? }

}

更改您的onNewIntent()名稱以調(diào)用此新方法:


@Override

protected void onNewIntent(Intent intent) {

? ? super.onNewIntent(intent);

? ? onNewNfcTag(intent);

}

onCreate()從intent from調(diào)用這個相同的方法getIntent():


@Override

protected void onCreate(Bundle savedInstanceState) {

? ? // .... your code already here

? ? onNewNfcTag(getIntent());

}


查看完整回答
反對 回復(fù) 2023-06-21
  • 1 回答
  • 0 關(guān)注
  • 171 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號