簡體   English   中英

如何在Android登錄成功后開始新活動?

[英]How to start new activity after login success in Android?

我需要在登錄成功后開始新的活動。 我不知道從一個到另一個開始活動的代碼,以及如何在下面的代碼中執行它,我有代碼,這里是背景workers.java的代碼,我們有兩個面板:

package com.example.androidphp2016;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;

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 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_url1="http://adminlogin.php";
        String login_url2="http://studentlogin.php";
        if(type.equals("login1")) {
            try {
                String user_name = params[1];
                String password = params[2];
                URL url = new URL(login_url1);
                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) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if(type.equals("login2")) {
            try {
                String user_name = params[1];
                String password = params[2];
                URL url = new URL(login_url2);
                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) {
                e.printStackTrace();
            } catch (IOException e) {
                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) {
        super.onProgressUpdate(values);
    }
}

在你的onPostExecute方法中檢查結果是否正確,如果是,則登錄成功,然后添加代碼:

 Intent i = new Intent(getApplicationContext(), MainActivity.class);
                i.putExtra("caller", "LoginActivity");
                startActivity(i);
                finish();

我還建議您使用以下alertDialog.dismiss();解除alertDialog: alertDialog.dismiss();

我通過將此代碼添加到OnPostExecute中解決了這個問題,然后我在Manifest.xml中聲明了兩個活動。 感謝你們對我的幫助。

  protected void onPostExecute(String result) {

    if(result.equals("Admin login success")){

         Intent intent = new Intent(context, adminpanel.class);
         context.startActivity(intent);
    }
    if(result.equals("Student Login Success")){

     Intent intent = new Intent(context, studentpanel.class);
    context.startActivity(intent);
   }
    else
    {
        alertDialog.setMessage(result);
        alertDialog.show();
    }
}

好的,所以要打開新活動,你必須根據你的代碼使用這些代碼和適當的格式,

Intent i = new Intent(FromfirstActivity.this, TosecondActivity.class);
 startActivity(i);

它肯定會有所幫助,謝謝

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM