问题
I have a problem. I want to use a the method onNewIntent(...)
and getIntent(...)
.
Unfortunately the fragment do not know this intent. How can I use this method in my fragment?
This fragment should be able to read an NFC tag, so I need the intent.
I would be very happy to receive an answer. I thank you in advance. Below you can see my code.
public class UserFragmentGeldaufladen extends Fragment {
private Dialog epicDialog;
private NfcAdapter nfcAdapter;
private PendingIntent pendingIntent;
private boolean writeMode;
private IntentFilter writeTagFilters[];
private Context context;
private Tag myTag;
private interface RetrievalEventListener<T> {
abstract void onDataRetrieved(T t);
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_user_geldaufladen, container, false);
context = view.getContext();
epicDialog = new Dialog(view.getContext());
nfcAdapter = NfcAdapter.getDefaultAdapter(view.getContext());
if (nfcAdapter == null) {
// Stop here, we definitely need NFC
Toast.makeText(view.getContext(), "This device doesn't support NFC.", Toast.LENGTH_LONG).show();
}
// Error
readFromIntent(getIntent());
pendingIntent = PendingIntent.getActivity(this, 0, new Intent(context, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
IntentFilter tagDetected = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
tagDetected.addCategory(Intent.CATEGORY_DEFAULT);
return view;
}
private void readFromIntent(Intent intent) {
String action = intent.getAction();
if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(action)
|| NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)
|| NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {
Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
NdefMessage[] msgs = null;
if (rawMsgs != null) {
msgs = new NdefMessage[rawMsgs.length];
for (int i = 0; i < rawMsgs.length; i++) {
msgs[i] = (NdefMessage) rawMsgs[i];
}
}
buildTagViews(msgs);
}
}
private void buildTagViews(NdefMessage[] msgs) {
if (msgs == null || msgs.length == 0) return;
String text = "";
// String tagId = new String(msgs[0].getRecords()[0].getType());
byte[] payload = msgs[0].getRecords()[0].getPayload();
String textEncoding = ((payload[0] & 128) == 0) ? "UTF-8" : "UTF-16"; // Get the Text Encoding
int languageCodeLength = payload[0] & 0063; // Get the Language Code, e.g. "en"
// String languageCode = new String(payload, 1, languageCodeLength, "US-ASCII");
try {
// Get the Text
text = new String(payload, languageCodeLength + 1, payload.length - languageCodeLength - 1, textEncoding);
} catch (UnsupportedEncodingException e) {
Log.e("UnsupportedEncoding", e.toString());
}
System.out.println(text);
}
// Error
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
readFromIntent(intent);
if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) {
myTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
}
}
@Override
public void onPause(){
super.onPause();
WriteModeOff();
}
@Override
public void onResume(){
super.onResume();
WriteModeOn();
}
private void WriteModeOn(){
writeMode = true;
nfcAdapter.enableForegroundDispatch((Activity) context, pendingIntent, writeTagFilters, null);
}
/******************************************************************************
**********************************Disable Write*******************************
******************************************************************************/
private void WriteModeOff(){
writeMode = false;
nfcAdapter.disableForegroundDispatch((Activity) context);
}
}
回答1:
You can use a getIntent()
with Fragments but you need to call getActivity()
first. Something like getActivity().getIntent()...
could work.
回答2:
I wrote an example code to make an onNewIntent
method working.
Add an interface class to communicate your fragment activity with UserFragmentGeldaufladen fragment
public interface OnNewIntentListener {
void newIntent(Intent intent);
}
and in fragment activity, add interface variable and two methods
private OnNewIntentListener mOnNewIntentListener;
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
if (mOnNewIntentListener != null) {
mOnNewIntentListener.newIntent(intent);
}
}
public void setOnNewIntentListener(OnNewIntentListener onNewIntentListener) {
this.mOnNewIntentListener = onNewIntentListener;
}
Now, in UserFragmentGeldaufladen fragment, add onCreate method. When your activity is first time started, you must get intent from activity, like @Dante says
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//get first time intent
Intent intent = getActivity().getIntent();
((YourActivity)getActivity()).setOnNewIntentListener(new OnNewIntentListener() {
@Override
public void newIntent(Intent intent) {
//handle your code here for each new intent
}
});
}
To receive intent in onNewIntent
method, update your activity calling
Intent i = new Intent(context, YourActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
//i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // -> this will be require, if you will be update your fragment from BroadcastReceiver or Service
startActivity(i);
Hope that help you.
来源:https://stackoverflow.com/questions/62019071/use-onnewintent-in-a-fragment