登录后打开活动
Posted
技术标签:
【中文标题】登录后打开活动【英文标题】:Open activity after log in 【发布时间】:2017-10-15 20:33:03 【问题描述】:大家晚上好
我正在创建我的第一个 android studio 应用程序,但我确实部分登录,但我不知道如何在成功登录后打开新活动。谁能帮助我
这是后台任务:
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】:在您的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">
【讨论】:
我将相同的概念与我的项目中已经存在的活动提出,但它显示错误 它现在可以使用,但是它在成功注册后而不是在登录后打开活动 你必须在你的onPostExecute()
..上传递一个像Login successfully
...这样的字符串并开始新的活动以上是关于登录后打开活动的主要内容,如果未能解决你的问题,请参考以下文章