Android: Create An Intent IF a condition is met?

2019-09-02 11:47发布

问题:

Evening

Currently my system allows the user to enter their username and password, if these are entered correctly, their role will be selected from the database and displayed in a textview.

However I'm also attempting to allow the user to move to the menu page if their login details are correct. My issue is that the user is being redirected to the main page when they click the login button regardless of whether their details are correct. I attempted to surround the intent with an if clause however that had no affect.

    public class SigninActivity  extends AsyncTask<String,Void,String>{
    private TextView roleField;
    private Context context;


    public SigninActivity(Context context,TextView roleField) {
        this.context = context;
        this.roleField = roleField;
    }

    protected void onPreExecute(){

    }

    @Override
    protected String doInBackground(String... arg0) {

            try{
                String username = (String)arg0[0];
                String password = (String)arg0[1];

                String link = "*";
                String data  = URLEncoder.encode("username", "UTF-8") + "=" + URLEncoder.encode(username, "UTF-8");
                data += "&" + URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(password, "UTF-8");

                URL url = new URL(link);
                URLConnection conn = url.openConnection();

                conn.setDoOutput(true);
                OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());

                wr.write( data );
                wr.flush();

                BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));

                StringBuilder sb = new StringBuilder();
                String line = null;

                // Read Server Response
                while((line = reader.readLine()) != null)
                {
                    sb.append(line);
                    break;
                }
                return sb.toString();

            }
            catch(IOException e){
                return new String("Exception: " + e.getMessage());
            }

    }

    @Override
    protected void onPostExecute(String result){
        this.roleField.setText(result);

    }
   }

With the login calls the sign in constructor in the mainactvity page

public void loginPost(View view){

        String username = usernameField.getText().toString();
        String password = passwordField.getText().toString();
        new SigninActivity(this,role).execute(username,password);


        if(role!= null){
        Intent i = new Intent(this, MainScreen.class);
        startActivity(i);
    }else{
        Intent i = new Intent(this, MainActivity.class);
        startActivity(i);
    }

    }

回答1:

Evening

Here's not evening, it's now night :)

I attempted to surround the intent with an if clause however that had no affect.

Your logics to doing this (I mean logging a user in) is not correct. You should revise your codes.

First of all, AsyncTasks are not of type Activity. So, name SigninActivity is a bit misleading. Please rename it for experiencing better feelings about Android SDK programming.

The basic steps for implementing a typical login Activity is listed as follows. Please follow the steps.

  • Create a LoginActivity class and register it to AndroidManifest.xml as the default entry point to your app whenever users click on it in launcher.
  • In its onCreate check whether the user is already signed in or not.
  • If she is, redirect her to your main activity via:

    startActivity(new Intent(context, MainActivity.class));
    
  • If she is not, She should fill credentials fields you have presented for her by UI of your login activity.

  • Once she pressed Login button, show a so-called "Please Wait..." message for her to feeling better UX. Meanwhile you are starting LoginAsyncTask.

  • In its doInBackground contact your server or wherever you want for authenticating her. And return the result.

  • In its onPostExecute, first hide that "Please Wait..." message and then inform her of hers authentication.

  • If she was successfully authenticated, redirect her to your main activity.