0

抱歉再次问,我正在创建一个android应用程序来将数据写入标签,它可以感知标签并使用AAR自动打开,但是,在我填写完EdiText列后,它无法将数据写入文件和单击按钮,我尝试了不同的方法,包括在 layout.xml 文件中声明 onclick 操作,或使用 onclicklistener,但都不起作用,所以谁能帮我看看问题出在哪里?

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>

最近我的代码只能启动,但它无法将数据写入标签,因此当设备再次感应到标签时,它将再次打开这个应用程序,而不是我编写的另一个阅读标签应用程序。

4

1 回答 1

0

您的 IntentFilter 显然不匹配,使 ForegroundDispatch 不起作用。在您的onCreate()中,尝试添加tagDetected.addCategory(Intent.CATEGORY_DEFAULT);. 我认为这会奏效。

于 2012-07-13T05:55:16.680 回答