问题描述:

I have created a php page and a login page for verification but after login success it just pops up a login success message. I want my app to redirect to another activity after successfull login.how do i do it via 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);

}

}

网友答案:

In the onPostExecute() method you should start another activity like this:

                    Intent intent = new Intent(getActivity().getApplicationContext(), OtherActivity.class);
                    startActivity(intent);
网友答案:

In onPreExecute,onPostExecute you can access UI elements.

So you may do like this

    @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();     
            }
网友答案:

Try to specify the next flags to your intent

Intent i = new Intent(getBaseContext(), YourActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplication().startActivity(i);
相关阅读:
Top