Android NFC前台调度错误到两个相同活动的实例

时间:2017-12-19 03:50:58

标签: android nfc

我已经制作了使用NFC在Android中读卡的应用程序。应用程序通过在ReadActivity中使用NfcAdapter.enableForegroundDispatch()方法获取NFC标记数据,除了一种情况外它运行良好。

这是案例流程:

  1. 启动ReadActivity(首先是实例)

  2. 回到家

  3. 启动第三个通过意图调用ReadActivity的应用程序;

  4. 通过其次ReadActivity读取卡

  5. 奇怪的事情!!首先是ReadActivity实例 带到前面并获得了nfc数据!

  6. 这是部分代码:

    I:ReadActivity

    package com.song.nfctest;
    
    
    import android.app.Activity;
    import android.app.PendingIntent;
    import android.content.Intent;
    import android.content.IntentFilter;
    import android.nfc.NfcAdapter;
    import android.nfc.Tag;
    import android.nfc.tech.NfcB;
    import android.os.Bundle;
    import android.os.Parcelable;
    import android.util.Log;
    import android.view.Gravity;
    import android.widget.TextView;
    
    import java.io.IOException;
    
    
    public class ReadActivity extends Activity {
        private NfcAdapter mAdapter;
        private TextView textView;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
             textView = new TextView(this);
            textView.setGravity(Gravity.CENTER_HORIZONTAL);
    
            setContentView(textView);
    
            mAdapter = NfcAdapter.getDefaultAdapter(this);
            resolveIntent(getIntent());
    
            if (getCallingActivity() == null){
                textView.append("\ncalled by self");
            }else {
                textView.append("\ncalled by other app");
            }
    
            textView.append("\n\n\n\n\n\ntap your nfc tag to phone to continue ");
        }
        void resolveIntent(Intent intent) {
            printLog("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
            Parcelable p = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
            if (p == null) {
                return;
            }
            Tag nfcTag = (Tag) p;
            final NfcB nfcb = NfcB.get(nfcTag);
            try {
                nfcb.connect();
                printLog("----------- complete with  read logic----- ");
                textView.append("\n\n\n\n\n\n get nfc event !!!");
            } catch (IOException e1) {
                e1.printStackTrace();
            } finally {
                try {
                    nfcb.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        @Override
        public void onResume() {
            super.onResume();
            if (mAdapter == null) return;
            mAdapter.enableForegroundDispatch(this,
                    PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0),
                    new IntentFilter[]{new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED)},
                    new String[][]{new String[]{NfcB.class.getName()}});
        }
    
        @Override
        public void onNewIntent(Intent intent) {
            resolveIntent(intent);
        }
    
        @Override
        public void onPause() {
            super.onPause();
            if (mAdapter == null) return;
            mAdapter.disableForegroundDispatch(this);
        }
        void printLog(String message) {
            Log.e("krik", message);
        }
    }
    

    II:clientActivity:

    public class TestActivity extends Activity {
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            LinearLayout linearLayout = new LinearLayout(this);
    
            Button button = new Button(this);
            button.setText("call main app");
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    callApp();
                }
            });
            linearLayout.addView(button);
            setContentView(linearLayout);
        }
    
        private void callApp() {
            Intent intent = new Intent();
            intent.setComponent(new ComponentName("com.song.nfctest", "com.song.nfctest.ReadActivity"));
            startActivityForResult(intent, 1001);
    }
    }  
    

    PS1:我猜测它是由pendingIntent的requestCode引起的,我已经改变了它但最终没有用。

    PS2:我有一个非常简单的演示并上传到github:https://github.com/songsyl1207/nfctest.git。运行app模块和otherapp模块到Android设备,然后作为问题流程运行,你会看到它。

    希望有人可以帮助我!

0 个答案:

没有答案