1

(using 4.1.1 JellyBean on a Nexus S) I am trying to follow these instructions, but the restrictions on in which activities setNdefPushMessage() can be used are to me somewhat vague. Presently my app, which has an activity for handling http requests via:

<activity android:name=".HandleUrlActivity"
          android:label="@string/title_handle_url_message" >
    <intent-filter>
        <action android:name="android.intent.action.VIEW" /
        <category android:name="android.intent.category.DEFAULT" />
        <data android:scheme="http" />
    </intent-filter>
</activity>

crashes with:

java.lang.IllegalStateException: API cannot be called while activity is paused

when setNdefPushMessage() is called within HandleUrlActivity().

In what activities can setNdefPushMessage() be used? Or how can I keep an activity running until the Ndef is served and how do I know when that activity can return/exit? Calling:

    NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
    nfcAdapter.setNdefPushMessage(ndef, this);
    nfcAdapter.wait();

gives the above error with or without the wait. Clearly setNdefPushMessage() needs some support mechanisms to handle the wait-for-nfc-receive-capable-device-proximity and then send events and I expected it to block until all this was complete, but apparently it does not.

(enableForegroundNdefPush/disableForegroundNdefPush apparently addressed this issue but appear to have been deprecated for ICS/JB with claims that setNdefPushMessage handles all this...)

4

1 回答 1

0

What are you trying to do? setNdefPushMessage() simply registers a message, it will only be sent when the Android runtime detects a compatible device. You have no control over this, so there is no point in calling wait(), etc (generally you shouldn't try to control the activity lifecycle). Calling it in onCreate() should work, but not calling it in the constructor when the activity is not yet full constructed and there is no valid Context. Generally though, having this in an activity that handles VIEW doesn't make much sense, since you can't really control when it will be started, so what are you trying to accomplish?

于 2012-10-09T06:25:12.390 回答