通过 NFC 标签启动 Activity 时不调用 OnNewIntent
OnNewIntent not called when starting up an Activity through a NFC tag
我有一个应用程序可以从 NFC 标签读取数据并打印出来。按照此 https://developer.android.com/guide/topics/connectivity/nfc/nfc.html#filtering-intents(参见 ACTION_TECH_DISCOVERED 部分),当我从 android 本身扫描 NFC 标签时,我可以启动应用程序。但是,这不会触发 OnNewIntent 方法,该方法随后会解析并打印标记中包含的数据。仅当我在应用程序已打开时重新扫描标签才有效。
在主要活动中:
@Override
protected void onNewIntent(Intent intent) {
if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(intent.getAction())) {
//stuff
}
}
清单中:
<activity android:name=".MainActivity"
<intent-filter>
<action android:name="android.nfc.action.TECH_DISCOVERED" />
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data
android:name="android.nfc.action.TECH_DISCOVERED"
android:resource="@xml/filter_nfc"
/>
</activity>
问题是:是否可以直接触发OnNewIntent方法,还是有别的方法?
谢谢
试试这个:
protected void onCreate(Bundle savedInstanceState) {
//Your stuff
Intent intent = getIntent();
handleIntents(intent);
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
handleIntents(intent);
}
private void handleIntents(Intent intent) {
if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(intent.getAction())) {
//stuff
}
}
我有一个应用程序可以从 NFC 标签读取数据并打印出来。按照此 https://developer.android.com/guide/topics/connectivity/nfc/nfc.html#filtering-intents(参见 ACTION_TECH_DISCOVERED 部分),当我从 android 本身扫描 NFC 标签时,我可以启动应用程序。但是,这不会触发 OnNewIntent 方法,该方法随后会解析并打印标记中包含的数据。仅当我在应用程序已打开时重新扫描标签才有效。
在主要活动中:
@Override
protected void onNewIntent(Intent intent) {
if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(intent.getAction())) {
//stuff
}
}
清单中:
<activity android:name=".MainActivity"
<intent-filter>
<action android:name="android.nfc.action.TECH_DISCOVERED" />
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data
android:name="android.nfc.action.TECH_DISCOVERED"
android:resource="@xml/filter_nfc"
/>
</activity>
问题是:是否可以直接触发OnNewIntent方法,还是有别的方法?
谢谢
试试这个:
protected void onCreate(Bundle savedInstanceState) {
//Your stuff
Intent intent = getIntent();
handleIntents(intent);
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
handleIntents(intent);
}
private void handleIntents(Intent intent) {
if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(intent.getAction())) {
//stuff
}
}