Asynk任务android

发布于 2024-10-14 03:08:21 字数 1586 浏览 2 评论 0原文

我有一个具有多个活动的选项卡组。在其中一个选项卡中,我有两个活动,我想在它们之间放置一个进度对话框。为此,我使用 Asynk Task。以下是我的 AsynkTask 类,我为 AboutUs 活动创建了一个内部类:

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

    @Override
    protected void onPreExecute() {
        progDialog = ProgressDialog.show(AboutUs.this.getParent(), "Loading... ",
                "please wait....", true);
    }

    @Override
    protected Void doInBackground(Void... params) {
        final Intent aboutusIntent = new Intent(getParent(), Departments.class);
        final TabGroupActivity parentActivity = (TabGroupActivity)getParent();
        parentActivity.startChildActivity("Departments", aboutusIntent);
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {

        if(progDialog.isShowing())
        {
        progDialog.dismiss();
        }
    }
}  

我在 AboutUs 活动中调用该类:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.aboutus);
         .  
         .  
         .  
         .  
/* Button for going to Departments */
    Button ourdepbtn = (Button) findViewById(R.id.departmentsbutton);
    ourdepbtn.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            //ourDepartments();
            new TheTask().execute();
            return false;
        }
    });
}  

但是,这不会启动新的活动,即“部门”。进度对话框出现然后消失,但活动从未加载。
有什么建议吗??

I have a tabgroup having multiple activities. In one of the tabs i have two activities between whom i want to place a progress dialog.For this i am using Asynk Task. Following is my AsynkTask class which i have made an inner class for AboutUs activity:

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

    @Override
    protected void onPreExecute() {
        progDialog = ProgressDialog.show(AboutUs.this.getParent(), "Loading... ",
                "please wait....", true);
    }

    @Override
    protected Void doInBackground(Void... params) {
        final Intent aboutusIntent = new Intent(getParent(), Departments.class);
        final TabGroupActivity parentActivity = (TabGroupActivity)getParent();
        parentActivity.startChildActivity("Departments", aboutusIntent);
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {

        if(progDialog.isShowing())
        {
        progDialog.dismiss();
        }
    }
}  

I am calling this class in my AboutUs activity :

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.aboutus);
         .  
         .  
         .  
         .  
/* Button for going to Departments */
    Button ourdepbtn = (Button) findViewById(R.id.departmentsbutton);
    ourdepbtn.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            //ourDepartments();
            new TheTask().execute();
            return false;
        }
    });
}  

However this does'nt start a new activity i.e. Departments. The progress dialog appears and then disappears but activity never loads.
Any suggestions..??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

云雾 2024-10-21 03:08:21

首先,您无法从非 GUI 线程(Async doInBackground() 就是)启动活动。只需直接在 Button.onClick() (为什么使用 onTouch?)侦听器内启动即可。

如果您想尽快显示新活动的 ProgressDialog,则需要在新(子)活动 onCreate() 中创建它,因为您的 ProgressDialog 已连接到新(子)活动(是吗?) 。请注意创建布局的顺序(在调用 setContentView() 之后创建 ProgressDialog)。
我不太确定你为什么要显示 ProgressDialog。是否有什么东西延迟了 childActivity 的显示?您正在加载一些数据吗?然后,对话框应该与该加载任务相关(我猜是异步)。

First, you cannot start an activity from a non GUI thread (which Async doInBackground() is). Just start directly inside your Button.onClick() (why you use onTouch?) listener.

If you want to show up a ProgressDialog for the new Activity as soon as possible, you need to create it in the new (child) Activity onCreate(), as your ProgressDialog is connected to the new (child) activity (is it?). Take care about the order of creating layouts (create the ProgressDialog after calling setContentView()).
I am not very sure why you want to show that ProgressDialog. Is there something which delays the display of the childActivity? You loading some data? Then, the Dialog should be related to that loading task (Async I guess).

笔芯 2024-10-21 03:08:21

私有类 TheTask 扩展了 AsyncTask{

    Context con;
     Intent aboutusIntent;
     TabGroupActivity parentActivity;
    private TheTask(Context context)
    {
        this.con=context;
    }

    @Override
    protected void onPreExecute() {
        progDialog = ProgressDialog.show(con, "Loading... ",
                "please wait....", true);
    }

    @Override
    protected Void doInBackground(Void... params) {
         aboutusIntent = new Intent(con, Departments.class);
          parentActivity = (TabGroupActivity)getParent();
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        if(progDialog.isShowing())
        {
        progDialog.dismiss();
        }
        parentActivity.startChildActivity("Departments", aboutusIntent);

    }
}  

感谢您的建议奥利弗:)

private class TheTask extends AsyncTask{

    Context con;
     Intent aboutusIntent;
     TabGroupActivity parentActivity;
    private TheTask(Context context)
    {
        this.con=context;
    }

    @Override
    protected void onPreExecute() {
        progDialog = ProgressDialog.show(con, "Loading... ",
                "please wait....", true);
    }

    @Override
    protected Void doInBackground(Void... params) {
         aboutusIntent = new Intent(con, Departments.class);
          parentActivity = (TabGroupActivity)getParent();
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        if(progDialog.isShowing())
        {
        progDialog.dismiss();
        }
        parentActivity.startChildActivity("Departments", aboutusIntent);

    }
}  

Thanks for your suggestions Oliver :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文