Android:在NDEF_发现的操作上注册的活动被忽略
我正在开发一款应用程序,可以读取和处理NFC标签中的特定URI。我在NDEFèU Discovery上注册了一个“reader”活动(a),它从标签读取数据,然后启动一个与数据一起运行的“data handling”活动(B) 目前我有三个标记,每个标记具有不同的URI,更具体地说,具有相同的模式和路径,但具有不同的查询数据-->标记ID: T-1:mySchema://gman.com/path?id=T-1Android:在NDEF_发现的操作上注册的活动被忽略,android,android-intent,nfc,android-activity,Android,Android Intent,Nfc,Android Activity,我正在开发一款应用程序,可以读取和处理NFC标签中的特定URI。我在NDEFèU Discovery上注册了一个“reader”活动(a),它从标签读取数据,然后启动一个与数据一起运行的“data handling”活动(B) 目前我有三个标记,每个标记具有不同的URI,更具体地说,具有相同的模式和路径,但具有不同的查询数据-->标记ID: T-1:mySchema://gman.com/path?id=T-1 T-2:mySchema://gman.com/path?id=T-2 T-3:my
T-2:mySchema://gman.com/path?id=T-2
T-3:mySchema://gman.com/path?id=T-3
舱单:
<activity
android:label="@string/reader_nfc"
android:name=".reader.nfc.NfcReaderActivity"
android:stateNotNeeded="true" >
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="mySchema" />
</intent-filter>
</activity>
<activity
android:label="@string/data_manager_name"
android:name=".data.handlers.DataHandlerActivity" >
</activity>
}
这样做之后,我得到了一些额外的日志(不知道为什么,但我想这并不是那么重要),并且看到当我返回到第一个标记时,活动被重新启动(调用onRestart()),而在另外两种情况下,当扫描第二个和第三个标记时,活动被重新创建(调用onCreate())
调用onRestart()并从intent(readSource方法)检索数据时,getIntent()方法返回扫描前一个标记时收到的相同intent
我最近刚开始在Android上开发,我对这些概念不太熟悉,所以这可能是这里的核心问题;)。我试图弄明白这一点,但我就是找不到合乎逻辑的解释如果有人能在这里向我解释工作流程,我将不胜感激。
然而,这就是我解决这个问题的方法……因为reader活动可以作为一个单例,所以我将启动模式设置为singleTask
<activity
android:label="@string/reader_nfc"
android:name=".reader.nfc.NfcReaderActivity"
android:stateNotNeeded="true"
android:launchMode="singleTask" >
这对我来说很好,但我仍然想了解到底发生了什么,所以欢迎任何有用的(当然是)评论。您可能想了解活动生命周期和堆栈:
您可能需要了解活动生命周期和堆栈:
您能发布活动中的代码吗?我很确定你的问题出在java文件中。你能发布你活动中的代码吗?我很确定你的问题在java文件中。
<activity
android:label="@string/reader_nfc"
android:name=".reader.nfc.NfcReaderActivity"
android:stateNotNeeded="true"
android:launchMode="singleTask" >
public class NfcReaderActivity extends Activity {
private static final String TAG = "NfcReaderActivity";
/**
* override onNewIntent method and store the new intent as the current intent
*/
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
Log.d(TAG, "onNewIntent");
// set the intent as the current intent, so new data (EXTRA_NDEF_MESSAGES) can
//be accessed when calling getIntent() in readSource method
setIntent(intent);
}