我一直在尝试做的是发送一个整数并接收它,然后将该整数设置为倒计时的计时器。到目前为止,我能够发送整数并在另一台设备上打开应用程序,但是当设备加载活动时,它会打开 MainActivity 而不是 Newgame 活动。在这一点上我必须承认我的代码并不聪明,而且有点新手,但这里是处理 NFC 通信的代码摘录,该摘录来自 Newgame.java:
@Override
public NdefMessage createNdefMessage(NfcEvent event) {
int time = bomb1.getTimer();
String message = ( " " + time);
NdefMessage msg = new NdefMessage(
new NdefRecord[] { NdefRecord.createMime(
"application/vnd.com.Jhadwin.passthebomb.newgame" ,message.getBytes())
,NdefRecord.createApplicationRecord("com.Jhadwin.passthebomb")
});
return msg;
}
@Override
public void onResume() {
super.onResume();
// Check to see that the Activity started due to an Android Beam
if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) {
processIntent(getIntent());
}
}
@Override
public void onNewIntent(Intent intent) {
// onResume gets called after this to handle the intent
setIntent(intent);
}
/**
* Parses the NDEF Message from the intent and prints to the TextView
*/
void processIntent(Intent intent) {
Parcelable[] rawMsgs = intent.getParcelableArrayExtra(
NfcAdapter.EXTRA_NDEF_MESSAGES);
// only one message sent during the beam
NdefMessage msg = (NdefMessage) rawMsgs[0];
// record 0 contains the MIME type, record 1 is the AAR, if present
String newtimermsg = new String(msg.getRecords()[0].getPayload());
timeremtextview.setText(newtimermsg);
int newtimer = Integer.parseInt(newtimermsg);
bomb1.setTimer(newtimer);
bomb1.setState(true);
}
您可能会注意到此代码改编自 Google 网站上的 NFC 示例,如有任何帮助,我们将不胜感激。
还包括 AndroidManifest.xml 的应用程序部分
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.Jhadwin.passthebomb.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.Jhadwin.passthebomb.newgame"/>
<activity android:name="com.Jhadwin.passthebomb.About"/>
<activity android:name="com.Jhadwin.passthebomb.Help"/>
</application>