【问题标题】:How to Redirect to Another Activity From BackgroundWorker?如何从 BackgroundWorker 重定向到另一个活动?
【发布时间】:2023-03-20 21:27:01
【问题描述】:

我已经创建了一个 php 页面和一个用于验证的登录页面,但是登录成功后它只会弹出一个登录成功消息。我希望我的应用程序在成功登录后重定向到另一个活动。我如何通过后台工作人员进行操作

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

}

【问题讨论】:

    标签: php android redirect android-activity


    【解决方案1】:

    在 onPostExecute() 方法中,您应该像这样启动另一个活动:

                        Intent intent = new Intent(getActivity().getApplicationContext(), OtherActivity.class);
                        startActivity(intent);
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

        尝试为您的意图指定下一个标志

        Intent i = new Intent(getBaseContext(), YourActivity.class);
        i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        getApplication().startActivity(i);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-03-27
          • 1970-01-01
          • 1970-01-01
          • 2013-04-07
          相关资源
          最近更新 更多