很抱歉再次提问,我正在创建一个将数据写入标签的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>
最近我的代码只能启动,但无法将数据写入标签,因此当设备再次感知标签时,它会再次打开此应用,而不是我写的另一个阅读标签应用。
答案 0 :(得分:0)
您的IntentFilter显然不匹配,使ForegroundDispatch无法正常工作。
在onCreate()
中,尝试添加tagDetected.addCategory(Intent.CATEGORY_DEFAULT);
。我认为这样做会有所帮助。