Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/216.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 我的NFC书写应用程序无法写入标签_Android_Android Intent_Nfc - Fatal编程技术网

Android 我的NFC书写应用程序无法写入标签

Android 我的NFC书写应用程序无法写入标签,android,android-intent,nfc,Android,Android Intent,Nfc,抱歉再次询问,我正在创建一个android应用程序将数据写入标记,它可以感知标记并使用AAR自动打开,但是,在我填写EditText列并单击按钮后,它无法将数据写入文件,我尝试了不同的方法,包括在layout.xml文件中声明onclick操作,或者使用onclicklistener,但都不起作用,所以有人能帮我看看问题出在哪里吗 public class Writer extends Activity{ NfcAdapter mAdapter; PendingIntent mPendingI

抱歉再次询问,我正在创建一个android应用程序将数据写入标记,它可以感知标记并使用AAR自动打开,但是,在我填写EditText列并单击按钮后,它无法将数据写入文件,我尝试了不同的方法,包括在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);
}*/


}
以下是布局文件:



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

您的IntentFilter显然不匹配,导致ForegroundDispatch无法工作。
onCreate()
中,尝试添加
tagDetected.addCategory(Intent.CATEGORY\u默认值)。我认为这样就可以了。

但是如果它不工作,为什么它可以在检测到标签后启动,或者这个步骤与前台调度无关?因为它根本不会恢复,所以写入模式没有启用,因此无法写入任何内容?我只是想澄清我的想法,好问题。我认为,它是因为清单文件中的意图过滤器而启动的。根据活动的
launchMode
(例如singleTop),生成的调用将是
onCreate()
onNewIntent()
,或者可能是
onResume()
(我不太清楚;您可以通过在各种函数中显示Toast消息或登录到logcat进行检查。)好的,谢谢,我尝试先输出一些消息,以首先了解活动的流程
<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>