android片段中onnewintent()的替换方法

agyaoht7  于 2021-09-29  发布在  Java
关注(0)|答案(1)|浏览(406)

我正在使用碎片开发一个nfc应用程序。fragment类不允许我使用onnewintent()使应用程序扫描nfc标记。是否有替换功能或解决此问题的方法。下面是我在fragment类中使用的方法。我有两个片段负责不同的nfc功能。我需要能够单独使用片段生命周期管理nfc功能,而不是将数据传递给片段活动。

  1. @Override
  2. public void onCreate(Bundle savedInstanceState) {
  3. super.onCreate(savedInstanceState);
  4. if (getArguments() != null) {
  5. mParam1 = getArguments().getString(ARG_PARAM1);
  6. mParam2 = getArguments().getString(ARG_PARAM2);
  7. }
  8. _nfcAdapter = NfcAdapter.getDefaultAdapter(getActivity().getApplicationContext());
  9. CheckNfcStatus();
  10. NfcManager manager = (NfcManager) getActivity().getApplicationContext().getSystemService(Context.NFC_SERVICE);
  11. NfcAdapter adapter = manager.getDefaultAdapter();
  12. }
  13. @Override
  14. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  15. Bundle savedInstanceState) {
  16. // Inflate the layout for this fragment
  17. View v = inflater.inflate(R.layout.fragment_heart_beat, container, false);
  18. heartbearSearch = v.findViewById(R.id.heartBeat_Search);
  19. heartbearSuccess = v.findViewById(R.id.heartBeat_Success);
  20. heartbearSearch.setVisibility(View.VISIBLE);
  21. heartbearSuccess.setVisibility(View.GONE);
  22. return inflater.inflate(R.layout.fragment_heart_beat, container, false);
  23. }
  24. protected String TagUid;
  25. @Override
  26. public void onResume() {
  27. //CheckNfcStatus();
  28. AlertDialog alert = new AlertDialog.Builder(getActivity().getApplicationContext()).create();
  29. super.onResume();
  30. if (_nfcAdapter == null) { // checks if NFC is available on the device
  31. alert.setMessage("NFC is not supported on this device.");
  32. alert.setTitle("NFC Unavailable");
  33. alert.show();
  34. }
  35. else {
  36. IntentFilter tagDetected = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
  37. Intent intent = new Intent(getActivity().getApplicationContext(), getClass());
  38. intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
  39. final PendingIntent pendingIntent = PendingIntent.getActivity(getActivity().getApplicationContext(), 0, intent, 0);
  40. IntentFilter[] filters = new IntentFilter[]{
  41. new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED),};
  42. String[][] techList = new String[][]{
  43. new String[]{NfcA.class.getName()},
  44. new String[]{NfcB.class.getName()},
  45. new String[]{NfcF.class.getName()},
  46. new String[]{NfcV.class.getName()},
  47. new String[]{NfcBarcode.class.getName()
  48. },
  49. // Filter for nfc tag discovery
  50. };
  51. _nfcAdapter.enableForegroundDispatch(getActivity(), pendingIntent, filters, techList);
  52. }
  53. }
  54. @Override
  55. public void onPause() {
  56. super.onPause();
  57. _nfcAdapter.disableForegroundDispatch(getActivity());
  58. }
  59. @Override
  60. public void onNewIntent(Intent intent) {
  61. super.onNewIntent(intent);
  62. HBClass hb = new HBClass();
  63. if (intent.getAction() == NfcAdapter.ACTION_TECH_DISCOVERED) {
  64. Boolean res = true;
  65. if (res) {
  66. Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
  67. if (tag != null) {
  68. if (hb.ReadTagSync(tag)) {
  69. Context context = getContext();
  70. mheartImage.startAnimation(mHeartAnimation);
  71. CharSequence toastMessage = "Transmission successful";
  72. int duration = Toast.LENGTH_SHORT;
  73. Toast.makeText(context, toastMessage, duration).show();
  74. textView.setText("Scan a new Tag");
  75. }
  76. }
  77. }
  78. }
  79. }
  80. public void SendRWBool(boolean someval) {
  81. writeFlag = true;
  82. //WriteNFC(tg);
  83. }
  84. private void CheckNfcStatus() {
  85. Context context = getActivity().getApplicationContext();
  86. int duration = Toast.LENGTH_LONG;
  87. if (_nfcAdapter == null) {
  88. CharSequence toastMessage = "No NFC available for the device!";
  89. Toast.makeText(context, toastMessage, duration).show();
  90. // NFC is not available for device
  91. } else if (!_nfcAdapter.isEnabled()) {
  92. CharSequence toastMessage = "Please Enable NFC!";
  93. Toast.makeText(context, toastMessage, duration).show();
  94. // NFC is available for device but not enabled
  95. } else {
  96. CharSequence toastMessage = "NFC enabled!";
  97. Toast.makeText(context, toastMessage, duration).show();
  98. // NFC is enabled
  99. }
  100. }
mf98qq94

mf98qq941#

实际上,nfc是一个活动范围的操作,需要在您的活动中处理。
某些部分(如配置)可以在片段中完成,但与标记数据的实际交互应该在活动中完成,无论您使用的是manifest、foregrounddispatch还是enablereadermode。
你需要把你的想法转向它的头,以得到你想要的。
在活动中包含您的nfc处理代码,但要根据活动片段所做的事情设置条件。
e、 g.一些伪代码

  1. handleNfc() {
  2. if current fragment equal Fragment 1 {
  3. // Process NFC the Fragment 1 way
  4. } else {
  5. // Process NFC the Fragment 2 way
  6. }
  7. }

有多种方法可以实现这一点,其中片段与活动通信,在创建片段时将其转换为“当前”片段,活动将处理后的数据传回片段以供使用。
一些选项是活动到片段通信的接口。
或者我认为更好的方法是共享视图模型,因为当活动处理完nfc数据时,片段很容易观察到共享视图模型,从而得到通知。

展开查看全部

相关问题