Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/217.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
执行异步任务时Android自定义进度指示器_Android_Android Asynctask_Progress - Fatal编程技术网

执行异步任务时Android自定义进度指示器

执行异步任务时Android自定义进度指示器,android,android-asynctask,progress,Android,Android Asynctask,Progress,运行异步任务时,我希望显示进度指示器。这只股票很难看,所以我自己做了一个布局。但问题在于,它是一个单独的布局,这意味着我需要一个新的活动来显示它 我想我可以启动触觉(~progressActivity~),然后。。。摧毁它?但似乎不可能从父母那里阻止孩子的活动 我可以隐藏AsyncTask调用活动上的进度指示器,然后在处理时使其显示并隐藏布局的其余部分。现在我需要在我的主布局中有两个子布局,并且每次都隐藏一个子布局。这听起来像一个黑客,我不太喜欢它 有什么建议吗?newasynctask(){

运行异步任务时,我希望显示进度指示器。这只股票很难看,所以我自己做了一个布局。但问题在于,它是一个单独的布局,这意味着我需要一个新的活动来显示它

我想我可以启动触觉(~progressActivity~),然后。。。摧毁它?但似乎不可能从父母那里阻止孩子的活动

我可以隐藏AsyncTask调用活动上的进度指示器,然后在处理时使其显示并隐藏布局的其余部分。现在我需要在我的主布局中有两个子布局,并且每次都隐藏一个子布局。这听起来像一个黑客,我不太喜欢它

有什么建议吗?

newasynctask(){
new AsyncTask<Params, Progress, Result>() {
   Dialog dlg = new Dialog();
   View customProgress;
   public void onPreExecute() {
       //init your dialog here;
       View customProgress = LayoutInflater.from(CurrentActivity.this).inflate(R.layout.your_progress_layout, null, false);
       dialog.setContentView(view);
       dialog.show();
   }
   public Result doInBackground(Params... params) {
    // do something
    publishProgress(progress);  
   }
   public void onProgressUpdate(Progress progress) {
    // do something with progressView;
   }
   public void onPostExecute(Result result) {
      dlg.dissmiss();
      // process result;
   }
}.execute();
Dialog dlg=新建Dialog(); 查看进度; 公共无效onPreExecute(){ //在这里开始你的对话; 查看customProgress=LayoutInflater.from(CurrentActivity.this).充气(R.layout.your\u progress\u layout,null,false); setContentView(视图); dialog.show(); } 公共结果doInBackground(参数…参数){ //做点什么 出版进度(progress); } public void onProgressUpdate(进度){ //用progressView做某事; } 公共void onPostExecute(结果){ dlg.dissmiss(); //过程结果; } }.execute();

这是一种可能的方法(仅示例,可能包含错误)

如果您需要对ProgressBar进行更多控制,可以使用WindowManager在所有内容上添加视图。它可以在没有任何额外布局、活动或窗口的情况下完成。您可以像在常规视图中一样控制动画、触摸、位置和可见性。完全工作代码:

final ProgressBar view = new ProgressBar(TestActivity.this);
view.setBackgroundColor(0x7f000000);
final LayoutParams windowParams = new WindowManager.LayoutParams();
windowParams.gravity = Gravity.CENTER;
windowParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
windowParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
windowParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
        | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
        | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
windowParams.format = PixelFormat.TRANSLUCENT;
windowParams.windowAnimations = 0;

new AsyncTask<Integer, Integer, Integer>() {
    public void onPreExecute() {
        // init your dialog here;
        getWindowManager().addView(view, windowParams);
    }

    public void onPostExecute(Integer result) {
        getWindowManager().removeView(view);
        // process result;
    }

    @Override
    protected Integer doInBackground(Integer... arg0) {
        // do your things here
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
}.execute();
final ProgressBar视图=新的ProgressBar(TestActivity.this);
视图.setBackgroundColor(0x7f000000);
final LayoutParams windowParams=新建WindowManager.LayoutParams();
windowParams.gravity=gravity.CENTER;
windowParams.height=WindowManager.LayoutParams.WRAP_内容;
windowParams.width=WindowManager.LayoutParams.WRAP\u内容;
windowParams.flags=WindowManager.LayoutParams.FLAG\u不可聚焦
|WindowManager.LayoutParams.FLAG_不可触摸| WindowManager.LayoutParams.FLAG_保持屏幕打开
|WindowManager.LayoutParams.FLAG_布局在屏幕中;
windowParams.format=PixelFormat.半透明;
windowParams.windowAnimations=0;
新建异步任务(){
公共无效onPreExecute(){
//在这里开始你的对话;
getWindowManager().addView(视图,windowParams);
}
public void onPostExecute(整数结果){
getWindowManager().removeView(视图);
//过程结果;
}
@凌驾
受保护的整数doInBackground(整数…arg0){
//在这里做你的事情
试一试{
睡眠(3000);
}捕捉(中断异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
返回null;
}
}.execute();

不要使用丑陋的对话框,创建美丽的:)自定义对话框非常好,正是我需要的!