Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/201.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android NFC标签';在后台从应用程序中发现时,s intent extra将丢失_Android_Android Fragments_Android Intent_Nfc_Intentfilter - Fatal编程技术网

Android NFC标签';在后台从应用程序中发现时,s intent extra将丢失

Android NFC标签';在后台从应用程序中发现时,s intent extra将丢失,android,android-fragments,android-intent,nfc,intentfilter,Android,Android Fragments,Android Intent,Nfc,Intentfilter,我试图让我的应用程序打开一个特定的片段,然后处理该片段中的标记数据。当应用程序在前台运行时,它可以工作,但当我在后台发现标签时,它似乎丢失了额外的数据 当我读取标记时,应用程序被放在前台,调用onNewIntent,其中意图操作是android.nfc.action.TAG\u发现的。但是没有任何意图。当我直接从前景中检测到它时,会有额外的标签。。。我做错了什么 以下是我的代码中NFC特定的部分: 从前在我的活动中 @Override public void onNewIntent(Intent

我试图让我的应用程序打开一个特定的片段,然后处理该片段中的标记数据。当应用程序在前台运行时,它可以工作,但当我在后台发现标签时,它似乎丢失了额外的数据

当我读取标记时,应用程序被放在前台,调用
onNewIntent
,其中意图操作
android.nfc.action.TAG\u发现的
。但是没有任何意图。当我直接从前景中检测到它时,会有额外的标签。。。我做错了什么

以下是我的代码中NFC特定的部分:

从前在我的活动中

@Override
public void onNewIntent(Intent intent) {
    Log.e(TAG, "RETRIVE HERE" + selectedFragment.getTagText() );
    if (selectedFragment instanceof FragmentNfc) {
        Log.e(TAG, "RETRIVE HERE");
        FragmentNfc my = (FragmentNfc) selectedFragment;
        my.processNFC(intent);
    }
}

public static void setupForegroundDispatch(final Activity activity, NfcAdapter adapter) {
    final Intent intent = new Intent(activity.getApplicationContext(), activity.getClass());
    intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

    final PendingIntent pendingIntent = PendingIntent.getActivity(activity.getApplicationContext(), 0, intent, 0);
    adapter.enableForegroundDispatch(activity, pendingIntent, null, null);
}

public static void stopForegroundDispatch(final Activity activity, NfcAdapter adapter) {
    adapter.disableForegroundDispatch(activity);
}
从前,在我的片段中…(当我从后台检测到标签时,不会输入for循环)

public void processNFC(意图){
Log.e(标签“过程NFC”);
字符串hextump=“”;
String action=intent.getAction();
Log.e(标签,“动作:+动作);
if(NfcAdapter.ACTION_TAG_DISCOVERED.equals(ACTION)){
Tag Tag=intent.getParcelableExtra(NfcAdapter.EXTRA_Tag);
String[]techList=tag.getTechList();
字符串searchedTech=Ndef.class.getName();
用于(字符串技术:技术列表){
Log.e(标签“TECH:+TECH”);
如果(搜索技术等于(技术)){
byte[]tagId=intent.getByteArrayExtra(NfcAdapter.EXTRA_ID);
for(int i=0;i
从前在舱单上

   <activity
        android:name=".activityv2.ActivityHome"
        android:label="Security Agent"
        android:launchMode="singleInstance"
        android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.nfc.action.TECH_DISCOVERED" />
        </intent-filter>

        <meta-data
            android:name="android.nfc.action.TECH_DISCOVERED"
            android:resource="@xml/nfc_tag_filter" />
    </activity>

由于您在清单中注册了action
android.nfc.action.TECH\u DISCOVERED
的意图过滤器,因此方法
onNewIntent()
将接收
TECH\u DISCOVERED
意图,而不是
TAG\u DISCOVERED
意图。因此,if分支的条件

if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(action)) {
将计算为false,您将永远不会输入分支

您可以同时检查
TAG\u DISCOVERED
TECH\u DISCOVERED

if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(action) ||
    NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)) {

我找到了它,在我的清单中我使用了android.nfc.action.TECH_DISCOVERED和代码NfcAdapter.action_TAG_DISCOVERED。。。所以dumbTAG_DISCOVERED比TECH_DISCOVERED更适合使用?我最后在清单中添加了这个,但您的解决方案也有效…:@Jaythaking如果在清单中添加
TAG\u DISCOVERED
intent过滤器,请记住
TAG\u DISCOVERED
的含义与前台调度系统中使用的含义(全包)不同。相反,当在清单中使用
TAG_DISCOVERED
时,它只是一种后备机制(即捕获其他应用不需要的标记)。看到了吧。。。那么,当应用程序位于前台时,为什么会捕捉到TAG_?即使我没有在清单中添加这个,这也是意图中设置的操作。我希望它始终是TECH_discovered的,这是因为您为该intent filter注册了前台调度系统(通过指定null作为intent filter参数)
TAG_DISCOVERED
与前台调度具有“一网打尽”的含义。如果您想从前台调度系统接收
TECH\u DISCOVERED
,您必须在调用
enableForegroundDispatch()
时相应地设置意图过滤器。为什么我们需要
标记
if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(action) ||
    NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)) {