简体   繁体   中英

Call activity in method onPostExecute from asynctask class

I have problems using the method from the title. On my android app project i wanna use this method for launching a new activity when I receive a successful message from an .php script. The message is correctly received from the php script but i can't launch the new activity on method onPostExecute no matter what i have tried. This is how it look my code, can someone more experienced take look at it and tell me please what is wrong. Thanks in advance for your time.

public class BackGround extends AsyncTask<String, Void, String> {


    Context ctx;

  BackGround(Context  context)
    {
        this.ctx=context;
    }

    @Override
    protected String doInBackground(String... params) {
        String name = params[0];
        String password = params[1];

        String data = "";
        int tmp;

        try {
            URL url = new URL("http://webserver.xy/login_wellness.php");
            String urlParams = "email=" + name + "&password=" + password;

            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setDoOutput(true);

            OutputStream os = httpURLConnection.getOutputStream();
            os.write(urlParams.getBytes());
            os.flush();
            os.close();
            InputStream is = httpURLConnection.getInputStream();
            while ((tmp = is.read()) != -1) {
                data += (char) tmp;
            }
            is.close();
            httpURLConnection.disconnect();

            return data;

        } catch (MalformedURLException e) {
            e.printStackTrace();
            return "Exception: " + e.getMessage();
        } catch (IOException e) {
            e.printStackTrace();
            return "Exceptionn: " + e.getMessage();
        }

    }

    @Override
    protected void onPreExecute() {
    }

    @Override
    protected void onPostExecute(String result) {       
    if(result.equals("success"))

    {
        Toast.makeText(ctx,"onpost excute",Toast.LENGTH_LONG).show();        
        Intent intent = new Intent(ctx,MainActivity.class);
       // intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        ctx.startActivity(intent);  
    }

    Toast.makeText(ctx,result,Toast.LENGTH_LONG).show();
    }

}

Change your constructor as

BackGround(Context  context)
{
    this.ctx=context.getApplicationContext();
}

And then

Intent intent = new Intent(ctx,MainActivity.class);
//intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ctx.startActivity(intent); 

Hope this helps.

If you are using separate class,you just pass the class via constructor.

For example,

Your Async class :

your constructor :

BackGround(Context  context,Class mIntentclass) {
    this.ctx=context;
    this.mIntentclass = mIntentclass;

}

@Override protected void onPostExecute(String result) {

    if(result.equals("success")) {
    Toast.makeText(ctx,"onpost excute",Toast.LENGTH_LONG).show();        
    Intent intent = new Intent(ctx,mIntentclass.class);
    ctx.startActivity(intent);  

}
   Toast.makeText(ctx,result,Toast.LENGTH_LONG).show();
}

Your Need class :

you just call where you want it?

like

BackGround background = new BackGround(context,MainActivity);

Hope its help u!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM