Android的:创建一个意图如果条件得到满足? [英] Android: Create An Intent IF a condition is met?

查看:182
本文介绍了Android的:创建一个意图如果条件得到满足?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

晚上

目前我的系统允许用户来输入他们的用户名和密码,如果这些输入正确,它们的作用将被从数据库中选择并在一个TextView显示。

不过我也试图让用户如果他们的登录信息是正确的移动到菜单页面。我的问题是,用户被重定向到主页时,他们点击登录按钮,无论他们的详细信息是否正确。我试图环绕的意图与如果条款但是,这种没有任何影响。

 公共类SigninActivity扩展的AsyncTask<弦乐,太虚,字符串> {
    私人TextView的roleField;
    私人上下文的背景下;
    公共SigninActivity(上下文的背景下,TextView的roleField){
        this.context =背景;
        this.roleField = roleField;
    }    在preExecute保护无效(){    }    @覆盖
    保护字符串doInBackground(字符串...为arg0){            尝试{
                字符串username =(字符串)为arg0 [0];
                字符串密码=(字符串)为arg0 [1];                字符串link =*;
                字符串数据= URLEn coder.en code(用户名,UTF-8)+=+ URLEn coder.en code(用户名,UTF-8) ;
                数据+ =&放大器; + URLEn coder.en code(密码,UTF-8)+=+ URLEn coder.en code(密码,UTF-8);                网址URL =新的URL(链接);
                康涅狄格州的URLConnection = url.openConnection();                conn.setDoOutput(真);
                OutputStreamWriter WR =新OutputStreamWriter(conn.getOutputStream());                wr.write(数据);
                wr.flush();                读者的BufferedReader =新的BufferedReader(新的InputStreamReader(conn.getInputStream()));                StringBuilder的SB =新的StringBuilder();
                串线= NULL;                //读取服务器响应
                而((行= reader.readLine())!= NULL)
                {
                    sb.append(线);
                    打破;
                }
                返回sb.toString();            }
            赶上(IOException异常五){
                返回新的String(异常:+ e.getMessage());
            }    }    @覆盖
    保护无效onPostExecute(字符串结果){
        this.roleField.setText(结果);    }
   }

通过登录调用登录构造在mainactvity页

 公共无效loginPost(查看视图){        字符串username = usernameField.getText()的toString()。
        字符串密码= passwordField.getText()的toString()。
        新SigninActivity(这一点,角色).execute(用户名,密码);
        如果(角色!= NULL){
        意图I =新意图(这一点,MainScreen.class);
        startActivity(ⅰ);
    }其他{
        意图I =新意图(这一点,MainActivity.class);
        startActivity(ⅰ);
    }    }


解决方案

  

晚上


下面是不是晚上,它现在晚上:)


  

我试图环绕的意图与如果条款但是,这种没有任何影响。


您的逻辑来这样做(我的意思是登录的用户)是不正确的。你应该修改codeS。

首先,的AsyncTask 是键入活动不是。因此,名称 SigninActivity 是有点误导。请重新命名它经历有关Android SDK编程更好的感受。

实施一个典型的登录活动的基本步骤是如下。请按照下列步骤。


  • 创建一个 LoginActivity 类并将其注册到的Andr​​oidManifest.xml 作为默认的切入点,以您的应用程序的用户时点击它的发射。

  • 在其的onCreate 检查用户是否已经登录或没有。

  • 如果她是通过重定向她的主要活动:

      startActivity(新意图(背景下,MainActivity.class));


  • 如果她不是,她应该填写你presented她通过UI您登录活动的凭证领域。


  • 有一次,她pressed <大骨节病>登录按钮,显示出所谓的请等待......的消息让她感觉更好UX。同时要启动 LoginAsyncTask


  • 在其 doInBackground 请联系您的服务器或任何你想要的验证她。并返回结果。


  • 在其 onPostExecute ,先躲说:请稍候...消息,然后通知她她的身份验证。


  • 如果她成功通过身份验证,重定向她的主要活动。

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);
    }

    }

解决方案

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.

这篇关于Android的:创建一个意图如果条件得到满足?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆