我正在使用碎片开发一个nfc应用程序。fragment类不允许我使用onnewintent()使应用程序扫描nfc标记。是否有替换功能或解决此问题的方法。下面是我在fragment类中使用的方法。我有两个片段负责不同的nfc功能。我需要能够单独使用片段生命周期管理nfc功能,而不是将数据传递给片段活动。
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
_nfcAdapter = NfcAdapter.getDefaultAdapter(getActivity().getApplicationContext());
CheckNfcStatus();
NfcManager manager = (NfcManager) getActivity().getApplicationContext().getSystemService(Context.NFC_SERVICE);
NfcAdapter adapter = manager.getDefaultAdapter();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_heart_beat, container, false);
heartbearSearch = v.findViewById(R.id.heartBeat_Search);
heartbearSuccess = v.findViewById(R.id.heartBeat_Success);
heartbearSearch.setVisibility(View.VISIBLE);
heartbearSuccess.setVisibility(View.GONE);
return inflater.inflate(R.layout.fragment_heart_beat, container, false);
}
protected String TagUid;
@Override
public void onResume() {
//CheckNfcStatus();
AlertDialog alert = new AlertDialog.Builder(getActivity().getApplicationContext()).create();
super.onResume();
if (_nfcAdapter == null) { // checks if NFC is available on the device
alert.setMessage("NFC is not supported on this device.");
alert.setTitle("NFC Unavailable");
alert.show();
}
else {
IntentFilter tagDetected = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
Intent intent = new Intent(getActivity().getApplicationContext(), getClass());
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
final PendingIntent pendingIntent = PendingIntent.getActivity(getActivity().getApplicationContext(), 0, intent, 0);
IntentFilter[] filters = new IntentFilter[]{
new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED),};
String[][] techList = new String[][]{
new String[]{NfcA.class.getName()},
new String[]{NfcB.class.getName()},
new String[]{NfcF.class.getName()},
new String[]{NfcV.class.getName()},
new String[]{NfcBarcode.class.getName()
},
// Filter for nfc tag discovery
};
_nfcAdapter.enableForegroundDispatch(getActivity(), pendingIntent, filters, techList);
}
}
@Override
public void onPause() {
super.onPause();
_nfcAdapter.disableForegroundDispatch(getActivity());
}
@Override
public void onNewIntent(Intent intent) {
super.onNewIntent(intent);
HBClass hb = new HBClass();
if (intent.getAction() == NfcAdapter.ACTION_TECH_DISCOVERED) {
Boolean res = true;
if (res) {
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
if (tag != null) {
if (hb.ReadTagSync(tag)) {
Context context = getContext();
mheartImage.startAnimation(mHeartAnimation);
CharSequence toastMessage = "Transmission successful";
int duration = Toast.LENGTH_SHORT;
Toast.makeText(context, toastMessage, duration).show();
textView.setText("Scan a new Tag");
}
}
}
}
}
public void SendRWBool(boolean someval) {
writeFlag = true;
//WriteNFC(tg);
}
private void CheckNfcStatus() {
Context context = getActivity().getApplicationContext();
int duration = Toast.LENGTH_LONG;
if (_nfcAdapter == null) {
CharSequence toastMessage = "No NFC available for the device!";
Toast.makeText(context, toastMessage, duration).show();
// NFC is not available for device
} else if (!_nfcAdapter.isEnabled()) {
CharSequence toastMessage = "Please Enable NFC!";
Toast.makeText(context, toastMessage, duration).show();
// NFC is available for device but not enabled
} else {
CharSequence toastMessage = "NFC enabled!";
Toast.makeText(context, toastMessage, duration).show();
// NFC is enabled
}
}
1条答案
按热度按时间mf98qq941#
实际上,nfc是一个活动范围的操作,需要在您的活动中处理。
某些部分(如配置)可以在片段中完成,但与标记数据的实际交互应该在活动中完成,无论您使用的是manifest、foregrounddispatch还是enablereadermode。
你需要把你的想法转向它的头,以得到你想要的。
在活动中包含您的nfc处理代码,但要根据活动片段所做的事情设置条件。
e、 g.一些伪代码
有多种方法可以实现这一点,其中片段与活动通信,在创建片段时将其转换为“当前”片段,活动将处理后的数据传回片段以供使用。
一些选项是活动到片段通信的接口。
或者我认为更好的方法是共享视图模型,因为当活动处理完nfc数据时,片段很容易观察到共享视图模型,从而得到通知。