onPause() or this method will throw an IllegalStateException.
*
- * This method must be called from the main thread.
+ * This method must be called from the main thread. */ public void disableForegroundDispatch(Activity activity) { disableForegroundDispatchInternal(activity, false); } + class ForegroundDispatchPausedListener implements OnActivityPausedListener { + @Override + public void onPaused(Activity activity) { + disableForegroundDispatchInternal(activity, true); + } + } + void disableForegroundDispatchInternal(Activity activity, boolean force) { try { sService.disableForegroundDispatch(activity.getComponentName()); @@ -473,6 +475,58 @@ public final class NfcAdapter { } } + /** + * Enable NDEF messages push while this Activity is in the foreground. + */ + public void enableForegroundNdefPush(Activity activity, NdefMessage msg) { + if (activity == null || msg == null) { + throw new NullPointerException(); + } + if (!activity.isResumed()) { + throw new IllegalStateException("Foregorund NDEF push can only be enabled " + + "when your activity is resumed"); + } + try { + ActivityThread.currentActivityThread().registerOnActivityPausedListener(activity, + new ForegroundDispatchPausedListener()); + sService.enableForegroundNdefPush(activity.getComponentName(), msg); + } catch (RemoteException e) { + attemptDeadServiceRecovery(e); + } + } + + /** + * Disables foreground NDEF push setup with + * {@link #enableForegroundNdefPush}. + * + *
This must be called before the Activity returns from
+ * it's onPause() or this method will throw an IllegalStateException.
+ *
+ *
This method must be called from the main thread. + */ + public void disableNdefPushDispatch(Activity activity) { + disableForegroundDispatchInternal(activity, false); + } + + class ForegroundNdefPushPausedListener implements OnActivityPausedListener { + @Override + public void onPaused(Activity activity) { + disableNdefPushDispatchInternal(activity, true); + } + } + + void disableNdefPushDispatchInternal(Activity activity, boolean force) { + try { + sService.disableForegroundNdefPush(activity.getComponentName()); + if (!force && !activity.isResumed()) { + throw new IllegalStateException("You must disable forgeground NDEF push " + + "while your activity is still resumed"); + } + } catch (RemoteException e) { + attemptDeadServiceRecovery(e); + } + } + /** * Retrieve a TagTechnology object used to interact with a Tag that is * in field.