首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >让特定的代码在后台运行

让特定的代码在后台运行
EN

Stack Overflow用户
提问于 2012-05-24 16:21:12
回答 2查看 4K关注 0票数 3

我想让这段代码在后台运行。我不知道如何创建服务或Asynstask,因为它只需要这段代码,而不是所有其他代码。

代码语言:javascript
运行
复制
void StartTimer()
{
    int minsTicks=CountM*60*1000;
    int hoursTicks=CountT*60*60*1000;
    int totalTicks=hoursTicks+minsTicks;
    mTextField = (TextView) findViewById(R.id.TimerTextView);

    CountDownTimer aCounter = new CountDownTimer(totalTicks, 1000)
    {
         public void onTick(long millisUntilFinished) 
         {
             mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
         }
         public void onFinish() 
         {
            try 
            {
            mTextField.setText("Kaffe Maskinen er igang");
            mmOutputStream.write('2');
            Thread.sleep(900000);
            mmOutputStream.write('0');
            mTextField.setText("Kaffe Maskinen er slukket");
            } 
            catch (IOException e) {} catch (InterruptedException e) {}
         }
    };
     aCounter.start();
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-05-24 17:11:42

以下是AsyncTask的代码片段

代码语言:javascript
运行
复制
private class AsyncTaskEx extends AsyncTask<Void, Void, Void> {

    /** The system calls this to perform work in a worker thread and
    * delivers it the parameters given to AsyncTask.execute() */
    @Override
    protected Void doInBackground(Void... arg0) {
        StartTimer();//call your method here it will run in background
        return null;
    }

    /** The system calls this to perform work in the UI thread and delivers
    * the result from doInBackground() */
    @Override
    protected void onPostExecute(Void result) {
        //Write some code you want to execute on UI after doInBackground() completes
        return ;
    }

    @Override
    protected void onPreExecute() {
        //Write some code you want to execute on UI before doInBackground() starts
        return ;
    }
}

在你的Activity中编写这个类,然后调用你想要执行方法的地方

代码语言:javascript
运行
复制
new AsyncTaskEx().execute();
票数 8
EN

Stack Overflow用户

发布于 2012-05-24 17:18:09

你可以像这样在后台执行你的代码:

代码语言:javascript
运行
复制
private class GetLoginResponse extends AsyncTask<Void, Void, Boolean> {

    @Override
    protected void onPreExecute() {
        // whatever code u want to execute before background code
        // starts executing
    }

    @Override
    protected Boolean doInBackground(Void... params) {

        int minsTicks=CountM*60*1000;
        int hoursTicks=CountT*60*60*1000;
        int totalTicks=hoursTicks+minsTicks;
        mTextField = (TextView) findViewById(R.id.TimerTextView);

        CountDownTimer aCounter = new CountDownTimer(totalTicks, 1000) {

            public void onTick(long millisUntilFinished) {
                mTextField.setText("seconds remaining: "
                            + millisUntilFinished / 1000);
            }

            public void onFinish() {
                try {
                    mTextField.setText("Kaffe Maskinen er igang");
                    mmOutputStream.write('2');
                    Thread.sleep(900000);
                    mmOutputStream.write('0');
                    mTextField.setText("Kaffe Maskinen er slukket");
                } catch (IOException e) {
                } catch (InterruptedException e) {
                }
            }
        };

        aCounter.start();
    }

    @Override
    protected void onPostExecute(Boolean data) {
        // Here you can execute what you want to execute
        // after the background task completes
    }
}// end AsyncTask

new GetLoginResponse.execute();
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10733682

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档