java - 如何使 OnNewIntent 在选项卡内工作

标签 java android nfc onnewintent

我正在开发一个有 3 个选项卡的项目。第一个选项卡用于将信息写入 NFC 标签,其他 2 个选项卡用于从 NFC 标签读取信息。但是,我遇到了 OnNewIntent() 方法的问题。正如我从在线阅读中正确理解的那样,这种方法仅在 Activity 中使用,而不在 fragment 中使用。我看过类似的问题和答案,但我不完全明白在我的情况下需要做什么才能避免这个问题。

这是我在第一个选项卡中向 NFC 标签写入一些数据的代码:

public class Home extends Fragment {

NfcAdapter nfcAdapter;
Button writebtn;
Tag tag;
EditText txtName, txtCountry, txtID;

public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view,savedInstanceState);
    nfcAdapter = NfcAdapter.getDefaultAdapter(getContext());

    txtName = (EditText)view.findViewById(R.id.personName);
    txtCountry= (EditText)view.findViewById(R.id.personCountry);
    txtID= (EditText)view.findViewById(R.id.personID);
    writebtn=(Button)view.findViewById(R.id.nfcWriteBtn);

}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
    View v= inflater.inflate(R.layout.home, container,false);
    return v;
}

@Override
public void onResume() {
    super.onResume();
    if(nfcAdapter !=null){
        enableForegroundDispatchSystem();
    }
}

@Override
public void onPause() {
    super.onPause();
    disableForegroundDispatchSystem();
}

protected void onNewIntent(final Intent intent) {
    // super.onNewIntent(intent);
    getActivity().getIntent();

    if (intent.hasExtra(NfcAdapter.EXTRA_TAG)) {
        Toast.makeText(getContext(), "NFC tag discovered!", Toast.LENGTH_SHORT).show();

        tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
        writebtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                NdefMessage ndefMessage = createNdefMessage(txtName.getText().toString(), txtCountry.getText().toString(),txtID.getText().toString());

                writeNdefMessage(tag, ndefMessage);
            }
        });
    }

}

最佳答案

onNewIntent(Intent Intent) 属于 Activity 你不能将它放在 fragment 中。您可以做的就是每当在 Activity 中调用 onNewIntent(Intent Intent) 时将数据传递到您的 fragment 。要实现此目的,您需要重写 Activity 中的 onNewIntent() 并通知 fragment 有关 Intent 的信息。不要使用 getIntent() 使用参数中的 intent

 @Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    // Notify fragment about the new intent
}

您的 Fragments 方法应该如下所示。

 protected void onNewIntent( Intent intent) {
    if (intent.hasExtra(NfcAdapter.EXTRA_TAG)) {
        Toast.makeText(getContext(), "NFC tag discovered!", Toast.LENGTH_SHORT).show();
        tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
        writebtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                NdefMessage ndefMessage = createNdefMessage(txtName.getText().toString(), txtCountry.getText().toString(),txtID.getText().toString());
                writeNdefMessage(tag, ndefMessage);
            }
        });
    }

关于java - 如何使 OnNewIntent 在选项卡内工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49691830/

相关文章:

java - 当浏览器关闭时,如何让 Spring 生成的 cookie 过期?

java - Java 中的 C stroule 等价物

android - 具有自动滚动功能的水平 ListView

android - 如何从 Android 手机向 Pi 发送 NDEF 消息

java - hibernate 在选择期间锁定表多长时间?

java - If 语句错误

android - 如何在具有向后兼容性的 iOS6 中从全模式网站打开 native map

android - 只显示通知,后台应用保持 onResume

android - HCE是否可以双向通信?

android - 有没有Android Beam失败回调