0
對不起,再次問,我正在創建一個android應用程序將數據寫入標籤,它可以感應標籤並通過使用AAR自動打開,但是,它不能寫入數據填入EdiText列並點擊按鈕後,我已經嘗試了不同的方式,包括在layout.xml文件中聲明onclick動作,或者使用onclicklistener,但沒有一個可以工作,所以任何人都可以幫助我拍攝看看問題在哪裏?我的NFC寫作應用程序不能寫入標籤
public class Writer extends Activity{
NfcAdapter mAdapter;
PendingIntent mPendingIntent;
IntentFilter mWriteTagFilters[];
boolean mWriteMode;
Tag detectedTag;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_writer);
Button btn = (Button) findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
SetTag();
}
});
mAdapter = NfcAdapter.getDefaultAdapter(this);
mPendingIntent = PendingIntent.getActivity(this, 0,
new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
IntentFilter tagDetected = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
mWriteTagFilters = new IntentFilter[] { tagDetected };
//Intent intent = getIntent();
}
private void enableTagWriteMode(){
mWriteMode = true;
mAdapter.enableForegroundDispatch(this, mPendingIntent, mWriteTagFilters, null);
}
private void disableTagWriteMode(){
mWriteMode = false;
mAdapter.disableForegroundDispatch(this);
}
public void SetTag(){
EditText editText1 = (EditText) findViewById(R.id.edit_message1);
EditText editText2 = (EditText) findViewById(R.id.edit_message2);
String message1 = editText1.getText().toString();
String message2 = editText2.getText().toString();
byte[] textBytes1 = message1.getBytes();
byte[] textBytes2 = message2.getBytes();
NdefRecord textRecord1 = new NdefRecord(NdefRecord.TNF_WELL_KNOWN,
NdefRecord.RTD_TEXT, new byte[]{}, textBytes1);
NdefRecord textRecord2 = new NdefRecord(NdefRecord.TNF_WELL_KNOWN,
NdefRecord.RTD_TEXT, new byte[]{}, textBytes2);
NdefMessage mNdefMessage = new NdefMessage(
new NdefRecord[]{
textRecord1,
textRecord2,
NdefRecord.createApplicationRecord("android.reader")
}
);
writeTag(mNdefMessage, detectedTag);
}
public static void writeTag(NdefMessage message, Tag tag){
int size = message.toByteArray().length;
try {
Ndef ndef = Ndef.get(tag);
if (ndef != null){
ndef.connect();
if (ndef.isWritable() && ndef.getMaxSize() > size)
ndef.writeNdefMessage(message);
ndef.close();
}else{
NdefFormatable format = NdefFormatable.get(tag);
if (format != null) {
try {
format.connect();
format.format(message);
}catch(IOException e){
}
}
}
}catch(Exception e){
}
}
@Override
protected void onNewIntent(Intent intent){
if(NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction()))
detectedTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
}
@Override
public void onPause(){
super.onPause();
disableTagWriteMode();
}
@Override
public void onResume(){
super.onResume();
enableTagWriteMode();
}
/*@Override
public void onClick(View v) {
// TODO Auto-generated method stub
EditText editText1 = (EditText) findViewById(R.id.edit_message1);
EditText editText2 = (EditText) findViewById(R.id.edit_message2);
String message1 = editText1.getText().toString();
String message2 = editText2.getText().toString();
byte[] textBytes1 = message1.getBytes();
byte[] textBytes2 = message2.getBytes();
NdefRecord textRecord1 = new NdefRecord(NdefRecord.TNF_WELL_KNOWN,
message1.getBytes(), new byte[]{}, textBytes1);
NdefRecord textRecord2 = new NdefRecord(NdefRecord.TNF_WELL_KNOWN,
message2.getBytes(), new byte[]{}, textBytes2);
NdefMessage mNdefMessage = new NdefMessage(
new NdefRecord[]{
textRecord1,
textRecord2,
NdefRecord.createApplicationRecord("android.reader")
}
);
writeTag(mNdefMessage, detectedTag);
}*/
}
以下是layot文件:
<EditText android:id="@+id/edit_message1"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="@string/edit_message1" />
<EditText android:id="@+id/edit_message2"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="@string/edit_message2" />
<Button android:id="@+id/button"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/button_send" />
</LinearLayout>
最近我的代碼只能推出,但它不能將數據寫入標籤,從而在設備感再次標籤,它會打開這個應用程序,而不是我寫的另一個閱讀標籤應用程序
但是,如果它不工作,爲什麼它可以在感應標籤後啓動,或者這個步驟與前臺調度無關?因爲它根本不能恢復,所以寫模式沒有被啓用,因此什麼都不能寫入?只是想先清除我的概念 – Conrad 2012-07-13 06:13:27
好問題。它是由於清單文件中的意圖過濾器而啓動的,我認爲。根據Activity的'launchMode'(例如singleTop),最終的調用將是'onCreate()','onNewIntent()'或者'onResume()'(我不知道確切的;你可以通過顯示Toast消息在各種功能或日誌記錄到日誌。) – 2012-07-13 08:05:40
好的,thx的傢伙,我嘗試輸出一些消息,首先找出活動的流程 – Conrad 2012-07-13 09:43:17