我想通过NFC从手机向另一个手机发送一个String,我有2个选项卡,它们是片段。一个选项卡上有一个发送按钮,另一个选项卡上有一个接收按钮。想法是,当我单击发送按钮时,它将触发NFC并发送String,因为其他移动设备可以单击按钮来接收,因此它将触发NFC来获取东西。然后它将在“接收”选项卡中显示为编辑文本。
非常感谢!
TAB 1
protected void onCreate(Bundle savedInstanceState) {
sendNFC = (Button)v.findViewById(R.id.SendNFC_button);
sendNFC.setOnClickListener(new View.OnClickListener() {
sendNFC.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String receive = getActivity().getIntent().getExtras().getString("TEXT");
EditText.setText(receive);
}
});
}
}
TAB 2
protected void onCreate(Bundle savedInstanceState) {
sendNFC = (Button)v.findViewById(R.id.SendNFC_button);
sendNFC.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendToNFC(v);
}
});
return v;
}
}
public void sendToNFC (View view){
Intent intent = new Intent(getActivity(), nfc_activity.class);
intent.putExtra("TEXT");
startActivity(intent);
}
NFC
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_nfc);
text = (TextView) findViewById(R.id.text);
nfcAdapter = NfcAdapter.getDefaultAdapter(this);
if (getIntent().getStringExtra("TEXT") != null) {
contentText = getIntent().getStringExtra("TEXT");
Intent intent = new Intent(this, TabsActivity.class);
intent.putExtra("TEXT");
startActivity(intent);
}
if (nfcAdapter == null) {
Toast.makeText(this, "No NFC", Toast.LENGTH_SHORT).show();
finish();
return;
}
pendingIntent = PendingIntent.getActivity(this, 0,
new Intent(this, this.getClass())
.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
}
@Override
protected void onResume() {
super.onResume();
if (nfcAdapter != null) {
if (!nfcAdapter.isEnabled())
showWirelessSettings();
nfcAdapter.enableForegroundDispatch(this, pendingIntent, null, null);
}
}
@Override
protected void onPause(){
super.onPause();
if (nfcAdapter != null){
nfcAdapter.disableForegroundDispatch(this);
}
}
@Override
protected void onNewIntent(Intent intent) {
setIntent(intent);
resolveIntent(intent);
}
private void resolveIntent(Intent intent) {
String action = intent.getAction();
if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(action)
|| NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)
|| NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {
Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
NdefMessage[] msgs;
if (rawMsgs != null) {
msgs = new NdefMessage[rawMsgs.length];
for (int i = 0; i < rawMsgs.length; i++) {
msgs[i] = (NdefMessage) rawMsgs[i];
}
} else {
byte[] empty = new byte[0];
byte[] id = intent.getByteArrayExtra(NfcAdapter.EXTRA_ID);
Tag tag = (Tag) intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
byte[] payload = dumpTagData(tag).getBytes();
NdefRecord record = new NdefRecord(NdefRecord.TNF_UNKNOWN, empty, id, payload);
NdefMessage msg = new NdefMessage(new NdefRecord[] {record});
msgs = new NdefMessage[] {msg};
}
displayMsgs(msgs);
}
}
private void displayMsgs(NdefMessage[] msgs) {
if (msgs == null || msgs.length == 0)
return;
StringBuilder builder = new StringBuilder();
List<ParsedNdefRecord> records = NdefMessageParser.parse(msgs[0]);
final int size = records.size();
for (int i = 0; i < size; i++) {
ParsedNdefRecord record = records.get(i);
String str = record.str();
builder.append(str).append("\n");
}
text.setText(builder.toString());
}
private void showWirelessSettings() {
Toast.makeText(this, "You must enable NFC", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(Settings.ACTION_WIRELESS_SETTINGS);
startActivity(intent);
}