任何人都可以告诉如何使用异步任务

时间:2012-10-20 06:17:27

标签: java android

任何人都可以告诉我如何登录?我尝试使用用户名Priyanka和密码Priyanka登录,但不成功。

任何人都可以帮助我吗?

package org.example.rig_parking;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

 public class Rig_ParkingActivity extends Activity {

String username,password;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


        Typeface myTypeface = Typeface.createFromAsset(getAssets(), "fonts/semibold.ttf");
        TextView myTextView = (TextView)findViewById(R.id.rig_parkmain);
        myTextView.setTypeface(myTypeface);
        TextView forget_main = (TextView)findViewById(R.id.forget_main);
        forget_main.setTypeface(myTypeface);
        final EditText email_main = (EditText)findViewById(R.id.email_main);
        email_main.setTypeface(myTypeface);
        final EditText password_main = (EditText)findViewById(R.id.password_main);
        password_main.setTypeface(myTypeface);

        Button copy=(Button) findViewById(R.id.copyright);
        copy.setText("(c)Rig Parking 2010-2012,All Rights Reserved");
        copy.setTypeface(myTypeface);

        TextView register=(TextView) findViewById(R.id.register_main);       
     register.setTypeface(myTypeface);

        register.setOnClickListener(new OnClickListener() {         
        public void onClick(View arg0) {

     Intent register=new Intent(getApplicationContext(),Register.class);    
     startActivity(register);               
        }
    });

 /*      forget_main.setOnClickListener(new OnClickListener() {         
        public void onClick(View arg0) {

     Intent register=new Intent(getApplicationContext(),Tab.class); 
     startActivity(register);               
        }
    });
        */
        TextView login=(TextView) findViewById(R.id.login_main);       
        login.setTypeface(myTypeface);
        login.setOnClickListener(new OnClickListener() {

        public void onClick(View arg0) {

            username=email_main.getText().toString();
            password=password_main.getText().toString();

            if(username.equals("")&&password.equals(""))
            {
                final AlertDialog alertDialog51;                    
                  alertDialog51 = new AlertDialog.Builder(Rig_ParkingActivity.this).create();
                    alertDialog51.setMessage(Html
                       .fromHtml("<font color='#00beb4'><b> Enter Email and Password</b></font>"));                                                                                                     
                    alertDialog51.setButton("Ok",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                int which) {                            
                        alertDialog51.dismiss();    
                               }
                         });            
                    alertDialog51.setButton2("Cancel", new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog,
                                            int which) {
                                        alertDialog51.dismiss();                                                
                       }
                    });                         
                    alertDialog51.show();                           
            }                                   
            else if(username.equals(""))
             {
                final AlertDialog alertDialog51;                    
                  alertDialog51 = new AlertDialog.Builder(Rig_ParkingActivity.this).create();
                    alertDialog51.setMessage(Html
                       .fromHtml("<font color='#00beb4'><b>    Enter Email    </b></font>"));                                                                                                       
                    alertDialog51.setButton("Ok",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                int which) {                            
                        alertDialog51.dismiss();    
                               }
                         });            
                    alertDialog51.setButton2("Cancel", new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog,
                                            int which) {
                                        alertDialog51.dismiss();                                                
                       }
                    });                         
                    alertDialog51.show(); 
             }
            else if(password.equals(""))
             {
                final AlertDialog alertDialog51;                    
                  alertDialog51 = new AlertDialog.Builder(Rig_ParkingActivity.this).create();
                    alertDialog51.setMessage(Html
                       .fromHtml("<font color='#00beb4'><b>    Enter  Password   </b></font>"));                                                                                                        
                    alertDialog51.setButton("Ok",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                int which) {                            
                        alertDialog51.dismiss();    
                               }
                         });            
                    alertDialog51.setButton2("Cancel", new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog,
                                            int which) {
                                        alertDialog51.dismiss();                                                
                       }
                    });                         
                    alertDialog51.show(); 
             }
            else{
            LoginTask logintsk=new LoginTask();
            logintsk.execute();             
            }

        }
    });

    }
    private class LoginTask extends AsyncTask<Void, Void, Void> { 

        private final ProgressDialog dialog = new ProgressDialog(Rig_ParkingActivity.this);

        @Override
    protected void onPreExecute() { 

            this.dialog.setMessage("Loading ..."); 
            this.dialog.show(); 
        } 


    @Override
    protected Void doInBackground(final Void... unused) { 

      Log.i("Priyanka", "username"+username);
      Log.i("Priyanka", "password"+password);
      String auth=BusinessLogic_login.candidatelogin(username, password); 
      String userid=auth; 
        Log.i("priyanka", "auth"+auth);
        if(auth.equals("0")||(auth.equals("")))
        {
            Rig_ParkingActivity.this.runOnUiThread(new Runnable() {             
                public void run() {                 
                    final AlertDialog alertDialog51;                    
                  alertDialog51 = new AlertDialog.Builder(Rig_ParkingActivity.this).create();
                    alertDialog51.setMessage(Html
                       .fromHtml("<font color='#00beb4'><b>Enter correct Email or Password</b></font>"));                                                                                                       
                    alertDialog51.setButton("Ok",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                int which) {                            
                        alertDialog51.dismiss();    
                               }
                         });            
                    alertDialog51.setButton2("Cancel", new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog,
                                            int which) {
                                        alertDialog51.dismiss();                                                
                       }
                    });                         
                    alertDialog51.show();               
                }
            });     
        }
        else {          

        Intent intnt=new Intent(getBaseContext(), Tab.class);
        startActivity(intnt);           

        }
        return null; // don't interact with the ui! 
    } 

    @Override
    protected void onPostExecute(Void result)  
    {  
            if (this.dialog.isShowing()) 
            { 
            this.dialog.dismiss(); 
            }
      } 
  }  
  }

1 个答案:

答案 0 :(得分:1)

 private class LoginTask extends AsyncTask<Void, Void, Void> { 

        private final ProgressDialog dialog = new ProgressDialog(Rig_ParkingActivity.this);

        @Override
    protected void onPreExecute() { 

            this.dialog.setMessage("Loading ..."); 
            this.dialog.show(); 
        } 


    @Override
    protected Void doInBackground(final Void... unused) { 

      Log.i("Priyanka", "username"+username);
      Log.i("Priyanka", "password"+password);
      String auth=BusinessLogic_login.candidatelogin(username, password); 
      String userid=auth; 
        Log.i("priyanka", "auth"+auth);
        if(auth.equals("0")||(auth.equals("")))
        {
           flag=true;
            Rig_ParkingActivity.this.runOnUiThread(new Runnable() {             
                public void run() {                 
                    final AlertDialog alertDialog51;                    
                  alertDialog51 = new AlertDialog.Builder(Rig_ParkingActivity.this).create();
                    alertDialog51.setMessage(Html
                       .fromHtml("<font color='#00beb4'><b>Enter correct Email or Password</b></font>"));                                                                                                       
                    alertDialog51.setButton("Ok",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                int which) {                            
                        alertDialog51.dismiss();    
                               }
                         });            
                    alertDialog51.setButton2("Cancel", new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog,
                                            int which) {
                                        alertDialog51.dismiss();                                                
                       }
                    });                         
                    alertDialog51.show();               
                }
            });     
        }

        return null; // don't interact with the ui! 
    } 

    @Override
    protected void onPostExecute(Void result)  
    {  
            if(!flag)
          {
           Intent intnt=new Intent(getBaseContext(), Tab.class);
          startActivity(intnt);
          }
            if (this.dialog.isShowing()) 
            { 
            this.dialog.dismiss(); 
            }
      } 
  }  

尝试此代码...拥有一个名为 flag boolean值,并将其设置为false。我刚刚使用过它..你必须初始化它。

onPostExecute()将在UI线程中运行..如果您在doInBackground()方法中调用intent,它在UIThread中没有更改,您无法看到更改是我从您的知识中获知的代码..检查这个