【发布时间】:2015-04-24 15:15:37
【问题描述】:
我正在尝试使用以下方式编写带有坐标(纬度和经度)的 NFC 标签:
这是在onCreate()里面:
btnWriteMap.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String latitude = lat.getText().toString();
String longitude = lon.getText().toString();
urlAddress = "geo:"+latitude+","+longitude;
TextView messageText = (TextView)findViewById(R.id.txtMessage);
messageText.setText("Touch NFC Tag to share GEO location\n"+
"Latitude: "+latitude+"\nLongitude: "+longitude);
}
});
mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
mFilters = new IntentFilter[] {
ndef,
};
mTechLists = new String[][] { new String[] { Ndef.class.getName() },
new String[] { NdefFormatable.class.getName() }};
onNewIntent() 方法:
@Override
public void onNewIntent(Intent intent) {
Log.i("Foreground dispatch", "Discovered tag with intent: " + intent);
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
String externalType = "nfclab.com:geoService";
NdefRecord extRecord = new NdefRecord(NdefRecord.TNF_EXTERNAL_TYPE, externalType.getBytes(), new byte[0], urlAddress.getBytes());
NdefMessage newMessage = new NdefMessage(new NdefRecord[] { extRecord});
writeNdefMessageToTag(newMessage, tag);
}
代码是书中的示例。我已经测试过,标签确实是用 geo:lat,lon 写的,当然还有我在 EditTexts 中的坐标。
当我阅读标签时出现问题。它只会显示(在默认的 Android 标记应用程序上)以下消息:
vnd.android.nfc//ext/nfclab.com:geoService
我想要的是让标签应用程序识别这些是谷歌地图坐标并使用坐标启动地图。 externalType String 应该包含什么? 我是否需要在清单中使用 Intent 过滤器?
【问题讨论】:
标签: android google-maps geolocation nfc ndef