我正在開發一款NFC應用程序。我正在使用一個例子,其中發送和接收數據由同一活動執行。現在我需要將發送到另一個,出於某種原因,接收不再工作。接收意向動作MAIN而不是NDEF消息
這裏是我的mainifest:
<uses-permission android:name="android.permission.NFC" />
<uses-feature
android:name="android.hardware.nfc"
android:required="true" />
<activity
android:name=".Screens.LogIn.LogInActivity"
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="text/plain" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
和接收活動:
public class LogInActivity extends AppCompatActivity {
public static final String TAG = LogInActivity.class.getSimpleName();
private String messagesToReceive= null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_log_in);
replaceFragment();
if (getIntent().getAction().equals(NfcAdapter.ACTION_NDEF_DISCOVERED)) {
handleNfcIntent(getIntent());
}
}
private void replaceFragment() {
android.support.v4.app.FragmentTransaction ft = this.getSupportFragmentManager().beginTransaction();
LogInFragment fragment = new LogInFragment();
ft.replace(R.id.fragmentFrame, fragment, LogInFragment.TAG);
ft.commit();
}
@Override
public void onResume() {
super.onResume();
handleNfcIntent(getIntent());
}
@Override
public void onNewIntent(Intent intent) {
handleNfcIntent(intent);
}
private void handleNfcIntent(Intent NfcIntent) {
if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(NfcIntent.getAction())) {
Parcelable[] receivedArray =
NfcIntent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
if(receivedArray != null) {
NdefMessage receivedMessage = (NdefMessage) receivedArray[0];
NdefRecord[] attachedRecords = receivedMessage.getRecords();
for (NdefRecord record:attachedRecords) {
String string = new String(record.getPayload());
if (string.equals(getPackageName())) { continue; }
messagesToReceive = string;
}
Toast.makeText(this, "Received " +
" Messages", Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(this, "Received Blank Parcel", Toast.LENGTH_LONG).show();
}
}
}
的問題是,NfcIntent.getAction()
總是等於android.intent.action.MAIN
,即使應用程序打開時,我與NFC發送數據。如您所見,清單中有動作NDEF_DISCOVERED
。
這是我送:
public NdefRecord[] createRecords() {
NdefRecord[] records = new NdefRecord[2];
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
byte[] payload = messagesToSend.
getBytes(Charset.forName("UTF-8"));
NdefRecord record = new NdefRecord(
NdefRecord.TNF_WELL_KNOWN, //Our 3-bit Type name format
NdefRecord.RTD_TEXT, //Description of our payload
new byte[0], //The optional id for our Record
payload); //Our payload for the Record
records[0] = record;
}
else {
byte[] payload = messagesToSend.//messagesToSendArray.get(i).
getBytes(Charset.forName("UTF-8"));
NdefRecord record = NdefRecord.createMime("text/plain",payload);
records[0] = record;
}
records[1] = NdefRecord.createApplicationRecord(getActivity().getPackageName());
return records;
}
@Override
public NdefMessage createNdefMessage(NfcEvent event) {
if (messagesToSend != null && !messagesToSend.equals("")) {
return null;
}
NdefRecord[] recordsToAttach = createRecords();
return new NdefMessage(recordsToAttach);
}
你在另一端發送了什麼數據? –
只是一個字符串。正如你可以在這裏看到的「android:mimeType =」text/plain「」 –
@MichaelRoland我編輯了我的帖子 –