我正在尝试学习NFC并阅读Google文档。我复制了他们的代码以创建一个Activty来将数据从一个Android设备发送到另一个Android设备。
我无法让它在线上运行
new NdefRecord[] { createMime(...
我从Google的示例代码中得到了“未定义的类型beamActivity”错误消息。
完整的代码在
下面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
// creat call back code
// gets called to star a beam, return messag to beam
@Override
public NdefMessage createNdefMessage(NfcEvent event) {
String text = ("Beam me up, Android!\n\n" +
"Beam Time: " + System.currentTimeMillis());
// error message hear
NdefMessage msg = new NdefMessage(
new NdefRecord[] { createMime(
"application/vnd.com.example.android.beam", text.getBytes())
/**
* The Android Application Record (AAR) is commented out. When a device
* receives a push with an AAR in it, the application specified in the AAR
* is guaranteed to run. The AAR overrides the tag dispatch system.
* You can add it back in to guarantee that this
* activity starts when receiving a beamed message. For now, this code
* uses the tag dispatch system.
*/
//,NdefRecord.createApplicationRecord("com.example.android.beam")
});
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 :(得分:2)
createMime(...)
是NdefRecord
的静态方法:
NdefMessage msg = new NdefMessage(
new NdefRecord[] {
NdefRecord.createMime(
"application/vnd.com.example.android.beam",
text.getBytes())
});