3

このコードをバックグラウンドで実行したいと思います。ほとんどの人が指摘するように、サービスまたは Asynstask を作成する方法がわかりません。これは、他のすべてではなく、このコードのみが必要なためです。

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();
}
4

2 に答える 2

8

これがのコードスニペットですAsyncTask

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記述し、メソッドを実行したい場所を呼び出します

new AsyncTaskEx().execute();
于 2012-05-24T09:11:41.573 に答える
2

次のように、コードをバックグラウンドで実行できます。

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();
于 2012-05-24T09:18:09.127 に答える