Search code examples
javaandroidmobileandroid-servicebarcode-scanner

Trying to pull plain text data to use in a service


I'm creating an app for an android mobile computer that can take the data scanned and convert a specified character to another. The way it currently works, I manually code in what characters to look for and convert to:

        public String specialWorkFor(String originalData, String codeType ){


        String[] targetStr = {"1", "2", "3"};
        String[] replaceStr = {"a","b","c"};

        String newData = "";
        newData = originalData;

        newData = ReplaceText(originalData,targetStr,replaceStr);
        return newData;
    }

    private static String ReplaceText (String originalData, String[] targetStr, String[] replaceStr ){
        String newData = "";
        String newDataTmp = "";
        newData = originalData;
        for (int i = 0; i < targetStr.length; i++){
            newDataTmp = newData.replace(targetStr[i], replaceStr[i]);
            newData = newDataTmp;
        }

        return newData;
    }

This works fine, but ideally I'd like to have an interface where I can just type into a plain text field and use the values from there to determine what characters get converted.

After creating the layout, I've tried doing this:

    //Inputs
    Context context1 = getApplicationContext();
    Activity act1=(Activity)context1;
    EditText codein = (EditText) act1.findViewById(R.id.input);
    String in = codein.getText().toString();

    //Outputs
    Context context2 = getApplicationContext();
    Activity act2=(Activity)context2;
    EditText codeout = (EditText) act2.findViewById(R.id.output);
    String out = codeout.getText().toString();

    public String specialWorkFor(String originalData, String codeType ){

        //String[] targetStr = {in};
        //String[] replaceStr = {out};

        String newData = "";
        newData = originalData;

        newData = ReplaceText(originalData,targetStr,replaceStr);
        return newData;
    }

Where I use:

    Context context1 = getApplicationContext();
    Activity act1=(Activity)context1;
    EditText codein = (EditText) act1.findViewById(R.id.input);
    String in = codein.getText().toString();

to pull values from the activity where I enter in my values. Issue is when I scan I get the error:

Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference

I am unsure where to go from here. Any thoughts?


Solution

  • You cannot cast application context to an activity. Create class Storage like this:

    public class Storage {
    
        public static final Storage instance = new Storage();
        public String codeIn = "";
        public String codeOut = "";
    }
    

    Add a textWatcher to both edittexts, and inside ontextchanged add :

    Storage.instance.codeIn = whatever is in coresponding edittext
    

    Inside the service you can get those values the same way:

    String in = Storage.instance.codeIn
    

    Also note that those values mast be set before the service uses them)))