Android 中的线程处理长时间运行的进程 [英] Threading in Android to process long running processes

查看:37
本文介绍了Android 中的线程处理长时间运行的进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,这是我的问题.我想学习 AsyncTask、Threading 和 Handler 来处理长时间运行的任务.我使用了 Android Cook Book 和 New Boston Android 教程,但我不能让它工作.

Ok, here is my problem. I want to learn AsyncTask, Threading and Handler to process long running tasks. I used Android Cook Book and New Boston Android tutorial, but I can't make it work.

我需要更改进度条消息.这不是为了项目,而是为了学习线程.线程对我来说真的很难.因此,如果您有任何了解线程的教程或指南,请告诉我.

I need to change the progress bar message. This is not for project, but to learn threading. Threading is really hard for me. So if you have any tutorial or guide to understand threading please let me know.

与此同时,我尝试了 Handler 类,但没有奏效.所以我尝试了 AsyncTask.但它也不起作用.

Meanwhile I tried Handler class, but it didn't work. So I tried AsyncTask. But it does not work either.

这是我的代码.

package com.example.androidpopup;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

    Button click_btn;
    ProgressDialog dialog;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        click_btn = (Button) findViewById(R.id.button1);

        click_btn.setOnClickListener(new OnClickListener() {

            public void onClick(View arg0) {
                dialog = ProgressDialog.show(MainActivity.this, "",
                        "Recording...", true);
                dialog.setCancelable(true);
                Thread thread = new Thread(){
                    public void run(){
                        try{
                            sleep(2000);
                            new UpdateUI().execute("testing");
                        }catch(InterruptedException e){
                            e.printStackTrace();
                        }
                    }
                };
            }
        });
    }

    public class UpdateUI extends AsyncTask<String, Integer, String>{

        @Override
        protected String doInBackground(String... params) {
            // TODO Auto-generated method stub
            return "Searching";
        }

        protected void onPreExecute(String f){
            //example of setting up something
            f = "whatever";
        }

        protected void onProgressUpdate(Integer...progress){

        }

        protected void onPostExecute(String result){
            dialog.setMessage(result);
        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

如果有人能解释我的方法有什么问题,那将非常有帮助.

If anyone can explain what's wrong of my approach it would be really helpful.

谢谢!

推荐答案

为什么要创建一个新线程只是为了等待 2 秒钟来调用 AsyncTask?我建议您删除该线程,通常调用 AsyncTask 并在其中放置 Thread.sleep(....).

Why are you creating a new Thread just to wait 2 seconds to call AsyncTask? I would suggest you to remove that Thread, normally call the AsyncTask and inside it place Thread.sleep(....).

类似于:

protected void onPreExecute(String f){
     Thread.sleep(2000);
     //example of setting up something
     f = "whatever";
}

PS:为了运行一个线程,你需要调用thread.start.;)

PS: in order to run a Thread, you need to call thread.start. ;)

这篇关于Android 中的线程处理长时间运行的进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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