Когда я сканирую метку NFC на телефоне, метод onNewIntent()
не вызывается. Это просто открытие панели, где я могу выбрать приложение, которое должно обрабатывать сканирование, но даже если я выберу свое приложение, метод onNewIntent()
не будет выполнен.
Я уже пытался поместить обработку тега NFC в дополнительный метод, называемый performTagOperations()
,
MainActivity:
public class MainActivity extends AppCompatActivity {
TextView mtv1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mtv1 = findViewById(R.id.tv1);
mtv1.setText("Hallo");
performTagOperations(getIntent());
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
Toast.makeText(this,"Intent",Toast.LENGTH_LONG).show();
mtv1.setText("Intent");
performTagOperations(intent);
}
private void performTagOperations(Intent intent){
if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
Log.d("NFC",tag.toString());
Parcelable[] rawMessages =
intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
if (rawMessages != null) {
NdefMessage[] messages = new NdefMessage[rawMessages.length];
for (int i = 0; i < rawMessages.length; i++) {
messages[i] = (NdefMessage) rawMessages[i];
}
// Process the messages array.
for (NdefMessage n:
messages) {
Log.d("NFC", n.toString());
}
}
}
}
}
AndroidManifest:
<uses-permission android:name="android.permission.NFC" />
<uses-feature android:name="android.hardware.nfc"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:ignore="GoogleAppIndexingWarning">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.nfc.action.TECH_DISCOVERED" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data
android:name="android.nfc.action.TECH_DISCOVERED"
android:resource="@xml/nfc_tech_filter" />
</activity>
</application>
nfc_tech_filter:
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
<tech-list>
<tech>android.nfc.tech.Ndef</tech>
<!-- class name -->
</tech-list>
</resources>
Он должен выполнять метод onNewIntent()
при сканировании тега, но не выполняет его.