登录后打开活动

时间:2017-05-16 16:29:42

标签: android mysqli phpmyadmin

每个人晚上好

我正在创建我的第一个Android工作室应用程序,我确实签了部分,但我不知道如何在成功登录后打开新活动。任何人都可以帮助我

这是后台任务:

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.os.AsyncTask;
import android.content.Context;
import android.os.AsyncTask;
import android.support.v7.app.AlertDialog;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;



public class BackgroundTask extends AsyncTask <String, Void, String>  {

    Context ctx;
    AlertDialog alertDialog;

    BackgroundTask(Context ctx)
    {
        this.ctx=ctx;
    }

    protected void onPreExecute() {
        super.onPreExecute();

        alertDialog=new AlertDialog.Builder(ctx).create();
        alertDialog.setTitle("Login Information......");
    }

    protected String doInBackground(String... params) {
        String reg_url = "http://10.0.2.2/webapp/register.php";
        String login_url = "http://10.0.2.2/webapp/login.php";
        String method = params[0];
        if (method.equals("register")) {
            String name = params[1];
            String user_name = params[2];
            String user_pass = params[3];

            try {
                URL url = new URL(reg_url);
                HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();

                httpURLConnection.setRequestMethod("POST");

                httpURLConnection.setDoOutput(true);
                OutputStream OS = httpURLConnection.getOutputStream();

                BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(OS, "UTF-8"));

                String data = URLEncoder.encode("user", "UTF-8") + "=" + URLEncoder.encode(name, "UTF-8") + "&" + URLEncoder.encode("user_name", "UTF-8") + "=" + URLEncoder.encode(user_name, "UTF-8") + "&" +
                        URLEncoder.encode("user_pass", "UTF-8") + "=" + URLEncoder.encode(user_pass, "UTF-8");

                bufferedWriter.write(data);
                bufferedWriter.flush();
                bufferedWriter.close();
                OS.close();
                InputStream IS = httpURLConnection.getInputStream();
                IS.close();
                return "Registration successfully.....";

            }
            catch (MalformedURLException e) {
                e.printStackTrace();
            }
            catch (IOException e) {
                e.printStackTrace();
            }


            return null;

        }
        else if(method.equals("login")){
            String login_name = params[1];
            String login_pass = params[2];
            try {
                URL url=new URL(login_url);
                HttpURLConnection httpURLConnection= (HttpURLConnection) url.openConnection();
                httpURLConnection.setRequestMethod("POST");
                httpURLConnection.setDoOutput(true);
                httpURLConnection.setDoInput(true);
                OutputStream outputStream= httpURLConnection.getOutputStream();
                BufferedWriter bufferedWriter=new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
                String data = URLEncoder.encode("login_name","UTF-8")+"="+URLEncoder.encode(login_name,"UTF-8")+"&"+URLEncoder.encode("login_pass","UTF-8")+"="
                        +URLEncoder.encode(login_pass,"UTF-8");
                bufferedWriter.write(data);
                bufferedWriter.flush();
                bufferedWriter.close();
                outputStream.close();

                InputStream inputStream= httpURLConnection.getInputStream();
                BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
                String response ="";
                String line="";
                while ((line=bufferedReader.readLine())!=null)
                {
                    response+=line;
                }
                bufferedReader.close();
                inputStream.close();
                httpURLConnection.disconnect();
                return response;
            }
            catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }



    protected void onProgressUpdate(Void... values) {
        super.onProgressUpdate(values);
    }


    protected void onPostExecute(String result) {
        if (result.equals("Registration successfully.....")) {

            Toast.makeText(ctx, result, Toast.LENGTH_LONG).show();



        } else {
            alertDialog.setMessage(result);
            alertDialog.show();

        }
    }
}

这是登录页面:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    EditText ET_NAME, ET_PASS;
    String login_name, login_pass;
    Intent intent;
Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    ET_NAME= (EditText)findViewById(R.id.user_name);
    ET_PASS= (EditText)findViewById(R.id.user_pass);
    button= (Button)findViewById(R.id.button);

        button.setOnClickListener(
                new View.OnClickListener() {

        @Override
        public void onClick(View v){
            long endTime = System.currentTimeMillis()+20*10;
            while(System.currentTimeMillis()< endTime){
                synchronized(this){
                    try{
                        wait(endTime - System.currentTimeMillis());


                    }catch (InterruptedException e ){
                        e.printStackTrace();
                    }
                }
                // textView.setText("Button pressed");
            }
        }
    }
        );
}


    public void userReg (View view)
    {
        startActivity(new Intent(this,Register.class));
    }

    public void userLogin (View view) {
        login_name=ET_NAME.getText().toString();
        login_pass=ET_PASS.getText().toString();
        String method ="login";
        BackgroundTask backgroundTask=new BackgroundTask(this);
        backgroundTask.execute(method,login_name,login_pass);

    }

}

1 个答案:

答案 0 :(得分:1)

onPostExecute()检查是否成功登录并开始新活动

protected void onPostExecute(String result) {
    if (result.equals("Registration successfully.....")) {

        Toast.makeText(ctx, result, Toast.LENGTH_LONG).show();
        Intent intent = new Intent(ctx,SecondActivity.Class);   //new Activity
        startActivity(intent);
    }
}
清单中的

:声明活动赞

<activity
  android:name=".SecondActivity">