【问题标题】:OnNewIntent not called when starting up an Activity through a NFC tag通过 NFC 标签启动 Activity 时未调用 OnNewIntent
【发布时间】:2018-01-28 17:32:07
【问题描述】:

我有一个从 NFC 标签读取数据并打印出来的应用。在此https://developer.android.com/guide/topics/connectivity/nfc/nfc.html#filtering-intents 之后(请参阅 ACTION_TECH_DISCOVERED 部分),当我从 android 本身扫描 NFC 标签时,我可以启动应用程序。但是,这不会触发 OnNewIntent 方法,该方法随后会解析并打印标签中包含的数据。仅当我在应用程序已打开时重新扫描标签时才有效。

在mainActivity中:

@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方法,还是有其他方法?

谢谢

【问题讨论】:

  • 您还需要在onCreateActivity/Fragment 中添加您的代码

标签: android android-intent nfc


【解决方案1】:

试试这个:

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
        }
}

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-04-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-17
  • 1970-01-01
相关资源
最近更新 更多