我已经创建了一个用于验证的php页面和一个登录页面,但在登录成功后,它只会弹出登录成功消息。我希望我的应用程序在成功登录后重定向到另一个活动。我是否通过backgroundworker
进行此操作package com.project.v_app;
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;
import android.app.AlertDialog;
import android.content.Context;
import android.os.AsyncTask;
public class BackgroundWorker extends AsyncTask<String, Void, String> {
Context context;
AlertDialog alertDialog;
BackgroundWorker (Context ctx){
context = ctx;
}
@Override
protected String doInBackground(String... params) {
String type = params[0];
String login_url = "http://192.168.0.103/login.php";
if(type.equals("login"))
{
try {
String user_name = params[1];
String password = params[2];
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 post_data = URLEncoder.encode("user_name","UTF-8")+"="+URLEncoder.encode(user_name,"UTF-8")+"&"
+URLEncoder.encode("password","UTF-8")+"="+URLEncoder.encode(password,"UTF-8");
bufferedWriter.write(post_data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
String result="";
String line="";
while((line = bufferedReader.readLine())!=null)
{
result +=line;
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return result;
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPreExecute() {
alertDialog = new AlertDialog.Builder(context).create();
alertDialog.setTitle("Login Status");
}
@Override
protected void onPostExecute(String result) {
alertDialog.setMessage(result);
alertDialog.show();
}
@Override
protected void onProgressUpdate(Void... values) {
// TODO Auto-generated method stub
super.onProgressUpdate(values);
}
}
答案 0 :(得分:0)
在onPostExecute()方法中,您应该启动另一个这样的活动:
Intent intent = new Intent(getActivity().getApplicationContext(), OtherActivity.class);
startActivity(intent);
答案 1 :(得分:0)
在onPreExecute,onPostExecute中,您可以访问UI元素。
所以你可能会喜欢这个
@Override
protected void onPreExecute() {
//will execute first
alertDialog = new AlertDialog.Builder(context).create();
alertDialog.setTitle("Login Status");
alertDialog.setMessage("Message");
alertDialog.show();
}
@Override
protected void onPostExecute(String result) {
//You may check some condition
Intent anothercallActivity=new Intent(context,ActivityClassName.class);
startActivity(anothercallActivity);
alertDialog.dismiss();
}
答案 2 :(得分:0)
尝试指定意图的下一个标志
Intent i = new Intent(getBaseContext(), YourActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplication().startActivity(i);