NFC标签未在onNewIntent上打开

时间:2019-03-28 14:40:50

标签: java android

我想将我的应用程序设置为打开我的应用程序,并在“ onNewIntent”上运行一些球型摄像机,但是NFC标签打开了我的应用程序,但跳过了onNewIntent。 这是一个空的nfc标签,我在其他应用程序中将其设置为打开我的应用程序,然后将我的应用程序设置为获取此mimeType。 还有其他需要在标签或代码问题上写的东西',我真的不知道下一步该怎么做。

这在我的AndroidManifest文件中:

<intent-filter>
            <action android:name="android.nfc.action.NDEF_DISCOVERED" />
            <data android:mimeType="application/com.calmdrives.calmdrives" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.nfc.action.TECH_DISCOVERED"/>
            <data android:mimeType="application/com.calmdrives.calmdrives" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.nfc.action.TAG_DISCOVERED"/>
            <data android:mimeType="application/com.calmdrives.calmdrives" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>

这是我的MainActivity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    nfcAdapter = NfcAdapter.getDefaultAdapter(this);
    Intent intent2 = new Intent(this, this.getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
    nfcPendingIntent = PendingIntent.getActivity(this, 0, intent2, 0);

    IntentFilter ndefDetected = new IntentFilter((NfcAdapter.ACTION_NDEF_DISCOVERED));
    IntentFilter discovery = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
    IntentFilter techDetected = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);

    readTagFilters = new IntentFilter[]{discovery, ndefDetected, techDetected};
}

@Override
protected void onResume() {
    super.onResume();
    if ((storedName.equals("") || storedCarNum.equals("")) && checkAndRequestPermissions()) {
        Intent intent = new Intent(this, SettingsActivity.class);
        finish();
        startActivity(intent);
    }
    performTagOperations(getIntent());
    IntentFilter tagDetected = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED); // filter for tags
    IntentFilter[] writeTagFilters = new IntentFilter[]{tagDetected};
    nfcAdapter.enableForegroundDispatch(this, nfcPendingIntent, readTagFilters, null);

}

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    String action = intent.getAction();
}

private void performTagOperations(Intent intent) {
    String action = intent.getAction();
    if (intent.getAction().equals(NfcAdapter.ACTION_TAG_DISCOVERED) ||
            intent.getAction().equals(NfcAdapter.ACTION_TECH_DISCOVERED)) {
            setIntent(intent);
        ... some other code
    }
}

0 个答案:

没有答案