从线程启动活动 [英] Start an activity from the thread

查看:157
本文介绍了从线程启动活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个生成的PDF文件的类。
这个类是通过活动,这意味着是具有的onCreate,在onPause(依此类推)的方法延长。

在一个PDF文件生成它需要生成,所以我需要显示一个进度条,通知该文件是在瞬间生成用户一段时间。

一切正常期望,当我点击一个按钮,生成进度条冻结几秒钟(根据LogCat中:在文件生成的进度条冻结)。

所以我的问题是,我不知道该怎么做,而该文件生成进度条不可阻挡的。

下面是我的code:

  Log.i(TAG,PDF生成:+位置);最后ProgressDialog对话框=新ProgressDialog(本);
dialog.setCancelable(真);
dialog.setIndeterminate(真);
dialog.setMessage(生成...);
dialog.show();新主题(新的Runnable接口(){    @覆盖
    公共无效的run(){        startActivity(新意图(getApplicationContext()
            PdfGenerator.class));        runOnUiThread(新的Runnable(){            @覆盖
            公共无效的run(){
                dialog.hide();
            }
        });    }})。开始();


解决方案

更改code作为启动使用runOnUiThread从主题活动:

  /// ...您的code在这里..    新主题(新的Runnable接口(){        @覆盖
        公共无效的run(){            Your_Current_Activity.this.runOnUiThread(新的Runnable(){                @覆盖
                公共无效的run(){                dialog.cancel();                //此处Start活动
               Your_Current_Activity.this.startActivity(新
                        意图(Your_Current_Activity.this,
                                       PdfGenerator.class));                }
            });        }    })。开始();

I have a class which generates a pdf file. This class is extended by Activity that means is have onCreate, onPause (and so on) methods.

While a pdf file is generating it takes some time to be generated so I need to show a progress bar to notify the user that the file is generating at the moment.

Everything works fine expect that when I click on a generate button the progress bar freezes for a few seconds (according to Logcat: progress bar freezing while file is generating).

So my problem is that, that I don't know how to make the progress bar unstoppable while the file is generating.

Below is my code:

Log.i(TAG, "PDF generation: " + position);

final ProgressDialog dialog = new ProgressDialog(this);
dialog.setCancelable(true);
dialog.setIndeterminate(true);
dialog.setMessage("Generating...");
dialog.show();

new Thread(new Runnable() {

    @Override
    public void run() {

        startActivity(new Intent(getApplicationContext(),
            PdfGenerator.class));

        runOnUiThread(new Runnable() {

            @Override
            public void run() {
                dialog.hide();
            }
        });

    }

}).start();

解决方案

Change your code as for starting an Activity from Thread using runOnUiThread:

///... your code here..

    new Thread(new Runnable() {

        @Override
        public void run() {

            Your_Current_Activity.this.runOnUiThread(new Runnable() {

                @Override
                public void run() {

                dialog.cancel();

                // START ACTIVITY HERE
               Your_Current_Activity.this.startActivity(new 
                        Intent(Your_Current_Activity.this,
                                       PdfGenerator.class));

                }
            });

        }

    }).start();

这篇关于从线程启动活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆