Search code examples
javaandroidprogram-entry-pointandroid-button

How to pause the execution of the entire program until a button is pressed in JAVA?


I have been making an android app in which I want to take advantage of some previously written Java code which is written entirely to run on its own with its own main method.

When I call the main method from my MainActivity.java's onCreate method it is still being called fine. But instead of interfacing with the console, I want the main() method to interface with the EditText, Buttons and views of android. MainActivity and the Java code are in separate classes.

What I want is basically the execution of the program to be stopped until I enter my entry in the EditText and resume when I press a button and then take the string I entered in the EditText. Just like in BufferdReader when we call the reader.readLine() method, the execution stops until pressed enter and then takes in whatever inputted before pressing enter.

There are three function calls one after another to promptString():

            case TdApi.AuthorizationStateWaitPhoneNumber.CONSTRUCTOR: {
                String phoneNumber = promptString("Please enter phone number: ");
                client.send(new TdApi.SetAuthenticationPhoneNumber(phoneNumber, false, false), new AuthorizationRequestHandler());
                break;
            }
            case TdApi.AuthorizationStateWaitCode.CONSTRUCTOR: {
                String code = promptString("Please enter authentication code: ");
                client.send(new TdApi.CheckAuthenticationCode(code, "", ""), new AuthorizationRequestHandler());
                break;
            }
            case TdApi.AuthorizationStateWaitPassword.CONSTRUCTOR: {
                String password = promptString("Please enter password: ");
                client.send(new TdApi.CheckAuthenticationPassword(password), new AuthorizationRequestHandler());
                break;
            }

Each call returns a string which is then sent for the API calls. If buffered reader is used the code will stop executing until someone enters a string and presses enter and correct value(according to the user) is inputted and then is returned by promptString() and then sent to API calls.

I want to replicate this buffered reader type functionality in android where when promptString() is called, the program waits until the user enters in the edit text and when a button is pressed then promptString() return the string and then further execution is carried on.

What is happening right now is that the execution proceeds without giving the user a chance to enter in the edit text and hence wrong (empty string) values are sent to the API calls and hence causing errors.

promptString() Code which is responsible for taking input and returning the inputted string (non modified):

private static String promptString(String prompt) {
        System.out.print(prompt);
        currentPrompt = prompt;
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String str = "";
        try {
            str = reader.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        currentPrompt = null;
        return str;
    }

I know what I ask is quite unconventional and stupid but I need this because I am facing some deadlines and I don't have time to fully understand the API calls.

If the question is unclear to you please comment that which part you want to be elaborated.

I am open to solutions which are not exactly as mine but will do the job that I am looking for and help me to let the program wait until the user is finished entering the string in the edit text.

I am using the telegram api's java example.

Please Help.


Solution

  • Android is event driven, you can replace the promptString behavior with something like this: and of course the promptString must have Android context, resist in an Activity or have access to Activity context

    private void promptString(String prompt, callback) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(prompt);
    
        // Set up the input
        final EditText input = new EditText(this);
        // Specify the type of input expected; this, for example, sets the input as a password, and will mask the text
        input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
        builder.setView(input);
    
        // Set up the buttons
        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                callback.setValue(input.getText().toString());
            }
        });
        builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });
    
        builder.show();
    }
    

    You can use the Java code as is but you have to modified some and call out to Android asking for input and at the same the hand over a callback handler that, when the input is done, will callback/send back the input value to use.

    Looking at the onAuthorizationStateUpdated() method in the java code, every switch case must call this method and supply the callback at the same this. So each switch case have it´s own callback or they can use the same and you can switch case again over the response from the AlertDialog, of course then you need some identifier for that, maybe easier to use different callback handler

        case TdApi.AuthorizationStateWaitPhoneNumber.CONSTRUCTOR: {
                        String phoneNumber = yourAndroidContextActivity.promptString 
       ("Please enter phone number: ", callback);
                        break;
                    }
    

    The code:

     client.send(new TdApi.SetAuthenticationPhoneNumber(phoneNumber, false, false), new AuthorizationRequestHandler());
    

    must go into the callback, there you go hope it helps