【问题标题】:Android NFC always calling onCreate when app is closed [duplicate]关闭应用程序时,Android NFC总是调用onCreate [重复]
【发布时间】:2019-08-26 13:31:48
【问题描述】:

我制作了检测 nfc 标签的应用程序。一切正常,当我的应用程序关闭并且我用手机扫描 nfc 标签时,它显示一个活动有 onCreate() 方法,当我再次扫描第二次它工作时,我不知道我在生命周期中是否出错该应用程序还是我错过了代码中的某些内容? 当我打开应用程序时,扫描正在工作:第一张照片 当应用程序关闭时第二张照片:从第二张照片开始,但在第二次扫描中它可以工作 这是我的代码

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);

    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
//        if ((intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) == 0) {
            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));
            }
      //  }
    }
}
<activity android:name=".Activities.NfcActivity" android:launchMode="singleTask">

    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    <intent-filter>
        <action android:name="android.nfc.action.NDEF_DISCOVERED" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
    <intent-filter>
        <action android:name="android.nfc.action.TECH_DISCOVERED" />
    </intent-filter>
</activity> 

【问题讨论】:

  • 我的问题是如何在我的应用关闭时第一次读取 nfc 标签而不显示具有 oncreate() 方法的活动
  • 我的应用程序正在从后台读取 nfc 标签,但在第二次扫描中,在第一次扫描中,它向我显示(第一次扫描)我的活动,文本视图具有 oncreate(),所以我只想了解如何从后台显示第一次扫描的结果

标签: java android nfc android-lifecycle rfid


【解决方案1】:

编辑:查看相关的完整解决方案:


您只是在第二次处理意图。

根据您当前的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() 以调用此新方法:

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    onNewNfcTag(intent);
}

onCreate() 调用这个相同的方法,意图来自getIntent()

@Override
protected void onCreate(Bundle savedInstanceState) {
    // .... your code already here
    onNewNfcTag(getIntent());
}

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多