我正在尝试使用http://www.tappednfc.com/wp-content/uploads/TAPPED-NFCDeveloperGuide-Part1.pdf上的文档来学习NFC。
我的class id定义了,我得到以下错误:
必须实现抽象方法NfcAdapter.OnNdefPushCompleteCallback.onNdefPushComplete
我定义了以下方法
public void OnNdefPushComplete( NfcEvent arg0)
{
mHandler.obtainMessage(1).sendToTarget();
}
这不是错误消息说我需要的回调吗?
完整的代码如下所示
public class BeamActivity extends Activity
implements CreateNdefMessageCallback,
OnNdefPushCompleteCallback {
NfcAdapter mNfcAdapter;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// see if there is a NFC interface
mNfcAdapter=NfcAdapter.getDefaultAdapter(this);
if (mNfcAdapter==null) Toast.makeText(this,"no adapter",
Toast.LENGTH_SHORT).show();
mNfcAdapter.setNdefPushMessageCallback(this,this);
mNfcAdapter.setOnNdefPushCompleteCallback(this,this);
}
public void OnNdefPushComplete( NfcEvent arg0) {
mHandler.obtainMessage(1).sendToTarget();
}
private final Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 1:
Toast.makeText(getApplicationContext(),"Mesg Sent",
Toast.LENGTH_SHORT).show();
break;
} // end switch
} // end handle mesg
}; // end new
// create call back code
public NdefMessage createNdefMessage(NfcEvent event) {
String text="hello world";
//NdefMessage msg = new NdefMessage(new NdefRecord[] {
// NfcUtils.
// }
return msg;
}
@Override
public void onNewIntent(Intent intent) {
setIntent(intent);
}
@Override
public void onResume() {
super.onRestart();
if (mNfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) {
processIntent( getIntent() );
}
}
void processIntent( Intent intet) {
Parcelable[] rawMsgs= intet.getParcelableArrayExtra(
mNfcAdapter.EXTRA_NDEF_MESSAGES );
NdefMessage msg = ( NdefMessage) rawMsgs[0];
String s= new String( msg.getRecords()[0].getPayload());
Toast.makeText(getApplicationContext(), s,
Toast.LENGTH_SHORT).show();
}
}
答案 0 :(得分:1)
请记住,Java区分大小写。方法OnNdefPushCompleteCallback.onNdefPushComplete(...)
以小写“o”开头(参见OnNdefPushCompleteCallback
的接口定义:
public void onNdefPushComplete(NfcEvent arg0) {
mHandler.obtainMessage(1).sendToTarget();
}