I need to fix the toast error R.string.error_notag R.string.ok_write R.string.ok_detected

0

package com.example.josue.project_nfc;

import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import java.io.IOException; import java.io.UnsupportedEncodingException;

import android.nfc.FormatException; import android.nfc.NdefMessage; import android.nfc.NdefRecord; import android.nfc.NfcAdapter; import android.nfc.Tag; import android.nfc.tech.Ndef; import android.os.Bundle; import android.annotation.SuppressLint; import android.app.Activity; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; import android.app.AlertDialog; import android.content.DialogInterface;

public class NFC extends AppCompatActivity {

@SuppressLint("NewApi")
public class MainActivity extends Activity {
    NfcAdapter adapter;
    PendingIntent pendingIntent;
    IntentFilter writeTagFilters[];
    boolean writeMode;
    Tag myTag;
    Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //Definimos el layout a usar
        setContentView(R.layout.activity_nfc);
        context = this;
        //Los elementos que vamos a usar en el layout
        Button btnWrite = (Button)findViewById(R.id.button);
        final TextView message = (TextView)findViewById(R.id.edit_message);
        //setOnCLickListener hará la acción que necesitamos
        btnWrite.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                try{
                    //Si no existe tag al que escribir, mostramos un mensaje de que no existe.
                    if(myTag == null){
                        Toast.makeText(context, context.getString(R.string.error_notag), Toast.LENGTH_LONG).show();
                    }else{
                        //Llamamos al método write que definimos más adelante donde le pasamos por
                        //parámetro el tag que hemos detectado y el mensaje a escribir.
                        write(message.getText().toString(),myTag);
                        Toast.makeText(context, context.getString(R.string.ok_write), Toast.LENGTH_LONG).show();
                    }
            }catch(IOException e){
                    Toast.makeText(context, context.getString(R.string.error_write),Toast.LENGTH_LONG).show();
                    e.printStackTrace();
                }catch(FormatException e){
                    Toast.makeText(context, context.getString(R.string.error_write), Toast.LENGTH_LONG).show();
                    e.printStackTrace();
                }
            }
        });
        adapter = NfcAdapter.getDefaultAdapter(this);
        pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this,getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
        IntentFilter tagDetected = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
        tagDetected.addCategory(Intent.CATEGORY_DEFAULT);
        writeTagFilters = new IntentFilter[]{tagDetected};
    }
    //El método write es el más importante, será el que se encargue de crear el mensaje
    //y escribirlo en nuestro tag.
    private void write(String text, Tag tag) throws IOException, FormatException{
        //Creamos un array de elementos NdefRecord. Este Objeto representa un registro del mensaje NDEF
        //Para crear el objeto NdefRecord usamos el método createRecord(String s)
        NdefRecord[] records = {createRecord(text)};
        //NdefMessage encapsula un mensaje Ndef(NFC Data Exchange Format). Estos mensajes están
        //compuestos por varios registros encapsulados por la clase NdefRecord
        NdefMessage message = new NdefMessage(records);
        //Obtenemos una instancia de Ndef del Tag
        Ndef ndef = Ndef.get(tag);
        ndef.connect();
        ndef.writeNdefMessage(message);
        ndef.close();
    }
    //Método createRecord será el que nos codifique el mensaje para crear un NdefRecord
    @SuppressLint("NewApi") private NdefRecord createRecord(String text) throws UnsupportedEncodingException{
        String lang = "us";
        byte[] textBytes = text.getBytes();
        byte[] langBytes = lang.getBytes("US-ASCII");
        int langLength = langBytes.length;
        int textLength = textBytes.length;
        byte[] payLoad = new byte[1 + langLength + textLength];

        payLoad[0] = (byte) langLength;

        System.arraycopy(langBytes, 0, payLoad, 1, langLength);
        System.arraycopy(textBytes, 0, payLoad, 1+langLength, textLength);

        NdefRecord recordNFC = new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_TEXT, new byte[0], payLoad);

        return recordNFC;

    }
    //en onnewIntent manejamos el intent para encontrar el Tag
    @SuppressLint("NewApi") protected void onNewIntent(Intent intent){
        if(NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())){
            myTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
            Toast.makeText(this, this.getString(R.string.ok_detected) + myTag.toString(), Toast.LENGTH_LONG).show();

        }
    }

    public void onPause(){
        super.onPause();
        WriteModeOff();
    }
    public void onResume(){
        super.onResume();
        WriteModeOn();
    }

    @SuppressLint("NewApi") private void WriteModeOn(){
        writeMode = true;
        adapter.enableForegroundDispatch(this, pendingIntent, writeTagFilters, null);
    }

    @SuppressLint("NewApi") private void WriteModeOff(){
        writeMode = false;
        adapter.disableForegroundDispatch(this);
    }
}
    }
    
asked by Josue Garcia 08.07.2018 в 20:24
source

1 answer

0

Go to Res - Values - strings.xml and add the following lines

<resources>
    <string name="error_notag">El tag es nulo</string>
    <string name="ok_write">El tag se escribio correctamente</string>
    <string name="error_write">Hubo un problema al escribir el tag</string>

</resources>
    
answered by 08.07.2018 / 20:45
source