gpt4 book ai didi

Android NFC Activity 正在重新启动,即使它位于堆栈的顶部

转载 作者:太空狗 更新时间:2023-10-29 16:40:27 25 4
gpt4 key购买 nike

我猜你们中的大多数人在看到这个标题时都会脸红,并且认为我们以前见过这个好吧我同意,但我已经尝试了一切来解决这个问题,但它不会补救。就这样吧。

我的应用程序可以从主屏幕(图标)启动,一切正常,它扫描 NFC 标签并将相关数据发送到服务器。然而,我还想要的是从 NFC 标签启动应用程序,这没有问题。然而!当应用程序通过 NFC 标签启动时,您然后取出手机并将其重新呈现给它应该的标签,然后读取标签并将数据发送到服务器。与通过图标启动应用程序的行为完全相同,但这次当您扫描 NFC 标签时,将启动应用程序的实例,最终将当前实例踢下堆栈,版本正忙于读取标签的实例,所以我收到了 ANR。

简而言之:
从主屏幕 -> 扫描标签 -> 应用启动。
然后重新扫描标签->
应用程序的当前实例消失并被新实例取代。我希望之前的实例留在原处并正常工作。

此时我的一些猜测是:-NFC 未正确注册到应用程序,因此操作系统执行默认行为。
- 当通过 NFC 标签启动时,有一个标志未正确设置,告诉操作系统该 Activity 不需要重新启动。

无论如何,这是代码:

<uses-permission android:name="android.permission.NFC"/>

<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="http"android:host="@string/URL_for_intent_filtering" android:pathPrefix="/id/" />

//Procedure of NFC startup called from OnCreate
private void startNFC()
{
mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
if(! mNfcAdapter.isEnabled())//check the NFC is switched on
{
mMainActivityHandler.sendEmptyMessage(NFC_DISABLED);
}

//its ok to carry on and instantiate the NFC even though its not enabled.
if(mNfcAdapter != null)
{
int requestID = (int) System.currentTimeMillis();
mPendingIntent = PendingIntent.getActivity(this, requestID, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), PendingIntent.FLAG_UPDATE_CURRENT);

IntentFilter intentF = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
intentF.addDataScheme("http");

intentfilter = new IntentFilter[] {intentF};



if(DEBUG) Log.i("DEBUG","+++++++++++++++++++++++++++++++++++++++++++++++ NFC HAS BEEN STARTED");

}else
{
//we have a problem with the NFC adapter probably because the device doesn't have one.
if(DEBUG) Log.i("DEBUG","+++++++++++++++++++++++++++++++++++++++++++++++ NFC HAS FAILED");
mMainActivityHandler.sendEmptyMessage(NFC_FAILED);
}
}

@Override
protected void onPause()
{
if(DEBUG) Log.i("MA","+++++++++++++++++++++++++++++++++++++++ ON PAUSE");
try{
mNfcAdapter.disableForegroundDispatch(this);
}catch(Exception e)
{

}
super.onPause();
}

//and here we should reinstate the NFC adapter
@Override
protected void onResume()
{
if(DEBUG) Log.i("MA","+++++++++++++++++++++++++++++++++++++++ ON RESUME");

//setupDataListener(true);
setupServerComms(getApplicationContext());
//mNfcAdapter.enableForegroundDispatch(this,mPendingIntent,intentfilter,null);
mNfcAdapter.enableForegroundDispatch(this,mPendingIntent,null,null);
super.onResume();
}

最佳答案

只需将 AndroidManifest.xml 中的这一行添加到您的 Activity 中,您就可能完成了。

        android:launchMode="singleTask"

关于Android NFC Activity 正在重新启动,即使它位于堆栈的顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18612360/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com