我正在尝试将 Android Beam 合并到我的应用程序中。但是,我需要为我的应用扩展另一个类而不是 Activity。当我尝试这样做时:
public class SomeClass extends AnotherActivity implements
CreateNdefMessageCallback{
Android Beam 根本不工作,Touch to Beam 不显示。但是,当我这样做时:
public class SomeClass extends Activity implements
CreateNdefMessageCallback{
Android Beam 工作正常。AnotherActivity 扩展了一些其他活动,这些活动最终扩展了 Activity 本身,所以我不知道为什么 Android Beam 在第一种情况下不起作用。有谁知道为什么会这样?
这是更多原始的 Android Beam 不工作代码供参考。
import android.app.Activity;
import android.content.Intent;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.NfcAdapter;
import android.nfc.NfcAdapter.CreateNdefMessageCallback;
import android.nfc.NfcEvent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Parcelable;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.view.View;
import android.view.View.OnClickListener;
public class SomeClass extends AnotherActivity implements
CreateNdefMessageCallback{
NfcAdapter mNfcAdapter;
TextView mInfoText;
private static final int MESSAGE_SENT = 1;
private NdefMessage message;
private TextView textView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Check for available NFC Adapter
mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
if (mNfcAdapter == null) {
Toast.makeText(this, "NFC is not available", Toast.LENGTH_LONG)
.show();
finish();
return;
}
// Register callback to set NDEF message
mNfcAdapter.setNdefPushMessageCallback(this, this);
}
/**
* Implementation for the CreateNdefMessageCallback interface
*/
@Override
public NdefMessage createNdefMessage(NfcEvent event) {
String text = ("Beam me up, Android!\n\n" +
"Beam Time: " + System.currentTimeMillis());
NdefMessage msg = new NdefMessage(
new NdefRecord[] {
NdefRecord.createMime(
"application/vnd.com.example.android.beam",
text.getBytes())
});
return msg;
}
@Override
public void onResume() {
super.onResume();
// Check to see that the Activity started due to an Android Beam
if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) {
processIntent(getIntent());
}
}
@Override
public void onNewIntent(Intent intent) {
// onResume gets called after this to handle the intent
setIntent(intent);
}
/**
* Parses the NDEF Message from the intent and prints to the TextView
*/
void processIntent(Intent intent) {
Parcelable[] rawMsgs = intent
.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
// only one message sent during the beam
NdefMessage msg = (NdefMessage) rawMsgs[0];
// record 0 contains the MIME type, record 1 is the AAR, if present
mInfoText.setText(new String(msg.getRecords()[0].getPayload()));
}
}
我在 DDMS LogCat 中检查了两者之间的差异。这是使用 AnotherActivity 的日志(NFC 不起作用)
07-14 13:53:10.317: W/NFC-LLC(608): LLC length mis-match
07-14 13:53:12.747: I/NFC-HCI(608): I'm P2P Passive Target @ 212 kb/s
07-14 13:53:12.754: D/NFCJNI(608): Discovered P2P Initiator
07-14 13:53:12.754: D/NFCJNI(608): 46
07-14 13:53:12.754: D/NFCJNI(608): 66
07-14 13:53:12.754: D/NFCJNI(608): 6d
07-14 13:53:12.754: D/NFCJNI(608): 01
07-14 13:53:12.754: D/NFCJNI(608): 01
07-14 13:53:12.754: D/NFCJNI(608): 11
07-14 13:53:12.754: D/NFCJNI(608): 03
07-14 13:53:12.754: D/NFCJNI(608): 02
07-14 13:53:12.754: D/NFCJNI(608): 00
07-14 13:53:12.754: D/NFCJNI(608): 13
07-14 13:53:12.754: D/NFCJNI(608): 04
07-14 13:53:12.754: D/NFCJNI(608): 01
07-14 13:53:12.754: D/NFCJNI(608): 96
07-14 13:53:12.754: D/NfcService(608): LLCP Activation message
07-14 13:53:12.762: I/NFCJNI(608): LLCP Link activated (LTO=150, MIU=128, OPTION=0x00, WKS=0x13)
07-14 13:53:12.762: I/NfcP2pLinkManager(608): LLCP activated
07-14 13:53:12.762: D/NfcP2pLinkManager(608): onP2pInRange()
07-14 13:53:12.778: D/NfcP2pLinkManager(608): onP2pSendConfirmationRequested()
07-14 13:53:12.934: I/NfcExecutionEnvironment(2915): NFCEE.open locked: stack=[NfcEventServiceWorker] openTid=263 nfceeOpened=false tryLock=0ms
07-14 13:53:12.942: D/NFCJNI(608): Unable to open SE connection, device already connected to a P2P peer or a Tag
07-14 13:53:13.950: D/NFCJNI(608): Unable to open SE connection, device already connected to a P2P peer or a Tag
07-14 13:53:14.958: D/NFCJNI(608): Unable to open SE connection, device already connected to a P2P peer or a Tag
07-14 13:53:15.965: D/NFCJNI(608): Unable to open SE connection, device already connected to a P2P peer or a Tag
07-14 13:53:16.981: D/NFCJNI(608): Unable to open SE connection, device already connected to a P2P peer or a Tag
07-14 13:53:17.989: D/NFCJNI(608): Unable to open SE connection, device already connected to a P2P peer or a Tag
07-14 13:53:18.997: D/NFCJNI(608): Unable to open SE connection, device already connected to a P2P peer or a Tag
07-14 13:53:20.012: D/NFCJNI(608): Unable to open SE connection, device already connected to a P2P peer or a Tag
07-14 13:53:20.801: I/NFCJNI(608): LLCP Link deactivated
07-14 13:53:20.809: D/NfcService(608): LLCP Link Deactivated message. Restart polling loop.
07-14 13:53:20.809: I/NfcP2pLinkManager(608): LLCP deactivated.
07-14 13:53:21.184: E/NfcExecutionEnvironment(2915): NFCEE.open() re-enabling card emulation
07-14 13:53:21.192: I/NfcAdapterExtras(2915): Wakelock WakeLock{41439a78 held=true, refCount=1} acquired 1405364001194 ms
07-14 13:53:21.192: I/NfcAdapterExtras(2915): Wakelock WakeLock{41439a78 held=true, refCount=1} released 1405364001198 ms elapsed 4 ms
07-14 13:53:21.192: I/NfcAdapterExtras(2915): Wakelock WakeLock{41439a78 held=true, refCount=1} acquired 1405364001199 ms
07-14 13:53:21.200: I/NfcAdapterExtras(2915): Wakelock WakeLock{41439a78 held=true, refCount=1} released 1405364001202 ms elapsed 3 ms
07-14 13:53:21.200: I/NfcExecutionEnvironment(2915): NFCEE.open ++: stack=[NfcEventServiceWorker, SecureElement] openTid=263 nfceeOpened=true holdCount=2
07-14 13:53:21.200: I/NfcExecutionEnvironment(2915): NFCEE.open ++: stack=[NfcEventServiceWorker, SecureElement, SecureElement] openTid=263 nfceeOpened=true holdCount=3
07-14 13:53:21.200: I/NfcExecutionEnvironment(2915): NFCEE.open ++: stack=[NfcEventServiceWorker, SecureElement, SecureElement, ControllerApplet] openTid=263 nfceeOpened=true holdCount=4
07-14 13:53:21.262: I/NfcExecutionEnvironment(2915): NFCEE.close --: stack=[NfcEventServiceWorker, SecureElement, SecureElement, ControllerApplet] openTid=263 nfceeOpened=true holdCount=4
07-14 13:53:21.262: I/NfcExecutionEnvironment(2915): NFCEE.close --: stack=[NfcEventServiceWorker, SecureElement, SecureElement] openTid=263 nfceeOpened=true holdCount=3
07-14 13:53:21.262: I/NfcExecutionEnvironment(2915): NFCEE.close --: stack=[NfcEventServiceWorker, SecureElement] openTid=263 nfceeOpened=true holdCount=2
07-14 13:53:21.262: W/NfcEventServiceWorker(2915): Payments are activated failure: com.google.android.apps.embeddedse.iso7816.SecureElementAppletFileNotFoundException: ControllerApplet: SELECT a0000004762010: statusWord=0x6a82 (File Not Found)
07-14 13:53:21.262: I/NfcExecutionEnvironment(2915): NFCEE.open ++: stack=[NfcEventServiceWorker, MifareManagerImpl] openTid=263 nfceeOpened=true holdCount=2
07-14 13:53:21.270: I/NfcExecutionEnvironment(2915): NFCEE.open ++: stack=[NfcEventServiceWorker, MifareManagerImpl, MifareManagerImpl] openTid=263 nfceeOpened=true holdCount=3
07-14 13:53:21.270: I/NfcExecutionEnvironment(2915): NFCEE.close --: stack=[NfcEventServiceWorker, MifareManagerImpl, MifareManagerImpl] openTid=263 nfceeOpened=true holdCount=3
07-14 13:53:21.270: I/NfcExecutionEnvironment(2915): NFCEE.open ++: stack=[NfcEventServiceWorker, MifareManagerImpl, MifareApplet] openTid=263 nfceeOpened=true holdCount=3
07-14 13:53:21.340: I/NfcExecutionEnvironment(2915): NFCEE.close --: stack=[NfcEventServiceWorker, MifareManagerImpl, MifareApplet] openTid=263 nfceeOpened=true holdCount=3
07-14 13:53:21.348: I/NfcExecutionEnvironment(2915): NFCEE.close --: stack=[NfcEventServiceWorker, MifareManagerImpl] openTid=263 nfceeOpened=true holdCount=2
07-14 13:53:21.364: D/NfcService(608): NFC-EE ON
07-14 13:53:21.372: D/NfcService(608): NFC-C ON
07-14 13:53:21.395: I/NfcExecutionEnvironment(2915): NFCEE.close closed: stack=[NfcEventServiceWorker] openTid=263 nfceeOpened=true tryLock=0ms session=8460ms
07-14 13:53:21.559: D/NfcP2pLinkManager(608): Debounce timeout
07-14 13:53:21.559: D/NfcP2pLinkManager(608): onP2pOutOfRange()
07-14 13:53:21.770: I/NfcEventServiceWorker(2915): Waiting ...