0

I'm trying to open settings on NFC Tap & Pay page with this piece of code:

startActivity(new Intent(Settings.ACTION_NFC_PAYMENT_SETTINGS));

While testing on LG Nexus 5X with Android 7.1.2 I have received this crash:

android.content.ActivityNotFoundException:
 No Activity found to handle Intent { act=android.settings.NFC_PAYMENT_SETTINGS }
  at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1809)
  at android.app.Instrumentation.execStartActivity(Instrumentation.java:1523)
  at android.app.Activity.startActivityForResult(Activity.java:4228)
  at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(SourceFile:50)
  at android.support.v4.app.FragmentActivity.startActivityForResult(SourceFile:79)
  at android.app.Activity.startActivityForResult(Activity.java:4186)
  at android.support.v4.app.FragmentActivity.startActivityForResult(SourceFile:859)
  at android.app.Activity.startActivity(Activity.java:4525)
  at android.app.Activity.startActivity(Activity.java:4493)
  at ...

Well, this crash can be handled easilly with try-catch but what is wierd, when I open this NFC settings manually, code works like a charm - no crash. Why? Does anyone have an explanation for this behavior?

In documentation[1] is written this:

In some cases, a matching Activity may not exist, so ensure you safeguard against this.

Is it possible that they meant this sentence like "you have to open settings manually, then it works fine"?

[1] https://developer.android.com/reference/android/provider/Settings.html#ACTION_NFC_PAYMENT_SETTINGS

JerabekJakub
  • 5,268
  • 4
  • 26
  • 33

1 Answers1

0

From: https://developer.android.com/reference/android/provider/Settings.html#ACTION_NFC_PAYMENT_SETTINGS

ACTION_NFC_PAYMENT_SETTINGS

added in API level 19

String ACTION_NFC_PAYMENT_SETTINGS

Activity Action: Show NFC Tap & Pay settings

This shows UI that allows the user to configure Tap&Pay settings.

In some cases, a matching Activity may not exist, so ensure you safeguard against this.

Input: Nothing

Output: Nothing

Constant Value: "android.settings.NFC_PAYMENT_SETTINGS"

ACTION_NFC_PAYMENT_SETTINGS is not supported by your device or can at least not be handled.

Update 1

Since your minAPILevel is 19, the action should be supported by the android RT. However, it is possible, that the link between the action and the NFC settings-menu, ALTHOUGH the menu exists, is not or can not be established.

Try to use Settings.ACTION_NFC_SETTINGS as the action and see if it starts. If so, I'd expect an implementation issue.

To guard against the exceptions, I'd recommend using:

PackageManager packageManager = getActivity().getPackageManager();
if (intent.resolveActivity(packageManager) != null) {
    startActivity(<your intent>);
} else {
    Log.d(TAG, "No application available to handle requested action.");
}

See: How to check if an intent can be handled from some activity? for credit and reference.

MABVT
  • 1,350
  • 10
  • 17
  • That is not true that it is not supported. Maybe it cannot be handled now but I'm asking why? When I open settings manually it works. It is though to show user a message "Go to setting, find something like Tap & Pay or Mobile payments or whatever your device vendor created". – JerabekJakub May 24 '17 at 09:53
  • Which MinSDKVersion and TargetSDKVersion do you use? Can you provide an actual code sample "around the call", i.e. where you call startActivity? – MABVT May 24 '17 at 10:47
  • minSDK = 19, targetSDK = 25. Code is inside onClickListener (using Butterknife). Just this line of code, there is nothing more. – JerabekJakub May 24 '17 at 11:30
  • @JerabekJakub See update. And I'll try to reconstruct the issue as well, as I have the same Android-Version. :D – MABVT May 24 '17 at 11:39
  • I cannot reproduce the issue anymore since I already went manually. Not it works so I cannot check your suggestion. :-( – JerabekJakub May 24 '17 at 11:42