繁体   English   中英

从asynctask类的onPostExecute方法中调用活动

[英]Call activity in method onPostExecute from asynctask class

我在使用标题中的方法时遇到问题。 在我的android应用项目中,当我从.php脚本收到成功消息时,我想使用此方法启动新活动。 从php脚本正确接收到该消息,但是无论我尝试了什么,我都无法在onPostExecute方法上启动新活动。 这就是我的代码的样子,更有经验的人可以看看它,然后告诉我哪里出了问题。 在此先感谢您的时间。

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

}

将您的构造函数更改为

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

接着

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

希望这可以帮助。

如果使用单独的类,则只需通过构造函数传递该类。

例如,

您的异步类:

您的构造函数:

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

您的需求课程:

你只是打电话给你想要的地方?

喜欢

BackGround background = new BackGround(context,MainActivity);

希望对你有帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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