[英]Listening to NFC with a pending intent and an intent
我有一個訂閱了ACTION_NDEF_DISCOVERED
的未決意圖和訂閱ACTION_TECH_DISCOVERED
的普通意圖的應用程序。
看起來我需要后一種意圖,以便我的應用程序將出現在 NFC 選擇操作屏幕中。
但是,根據我的待定意圖(以 API 參考代碼為模型),我必須掃描標簽兩次才能觸發OnNewIntent
。
於是產生了幾個疑惑:
這是核心問題:
我該如何進行這項工作,以便在應用程序之外,我的應用程序出現在 NFC 動作 window 中,而在應用程序內部, OnNewIntent
觸發只需要掃描?
根據您的描述,您的代碼設置看起來大部分都可以。 您可能錯過的一件事:當您的應用程序從 NFC 操作 window(應用程序選擇器)啟動時,您的應用程序的Activity
將使用onCreate()
啟動,並且您必須使用getIntent()
檢索 NFC 意圖。 只有在enableForegroundDispatch()
(使用PendingIntent
)之后,新的 NFC 意圖才會導致onNewIntent()
被調用。
你可以這樣做:
void onCreate(Bundle savedInstanceState) {
... // set up your Activity
handleNfcIntent(getIntent);
}
void onNewIntent(Intent intent) {
super.onNewIntent(intent);
handleNfcIntent(intent);
}
void handleNfcIntent(Intent intent) {
// NFC Intent handling code here
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.