【问题标题】:Android activity to handle multi threading处理多线程的 Android 活动
【发布时间】:2011-12-28 05:00:50
【问题描述】:

在我的活动课上,我想执行一系列长时间的计算,当我按下一个按钮时,这些计算需要大约 5 秒才能完成。因此,为了做到这一点,我创建了一个新类,它在其运行方法中执行所有计算(因为它实现了 Runnable),完成后我将变量设置为 true 以表明这一点。在检查按钮是否被按下的代码中,我启动了一个新线程,将我的类传入其中,然后检查 run 方法是否已完成。如果它完成了,我就打印数据。这样做的问题是,当我检查计算是否已经完成时,它们实际上还没有完成,所以它通过了那行代码并且从不打印数据。我试过做异步类方法,但我仍然认为它不起作用。当我按下按钮并继续检查是否已完成以便我可以打印数据时,有没有办法创建线程? Activity 中的哪段代码实际上被一遍又一遍地执行?感谢您提供任何信息。

if(v.equals(this.button)) {
             EditText param1 =  (EditText)findViewById(R.id.param1);
             EditText param2 = (EditText)findViewById(R.id.param2);

            calculations = new MathCalc(param1.getText().toString(), param2.getText().toString());

            new Thread(calculations).start();

            while(!calculations.isReady());

            Intent intent = new Intent(this,Show.class);
            intent.putExtra("show1", calculations.getResult());
            startActivity(intent);

            }

这是我想要实现的。

【问题讨论】:

    标签: android multithreading button asynchronous


    【解决方案1】:

    使用

    Button someButton = (Button) findViewById(R.id.favouriteButton);
    someButton.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View v) {
    
                    Thread thread = new Thread(new Runnable() {
                           @Override
                            public void run() {
    
                                while(!isDone){
                                   doAlotOfCalculations();
                                }
                            }
                    });
                    thread.start();
                  } 
    });
    
    
    
    
    private void doAlotOfCalculations(){
        ...    
        if(whenDone){
            isDone = true;
        }
        ....
    }
    

    Activity 中的哪段代码实际上被执行了并且 再来一遍?

    没有这样的事情。 每次启动(重新启动)此活动时执行的只是 onResume

    【讨论】:

    • 感谢您的回复。这可能有效,但事情变得更加复杂,因为当计算完成时我可能想要切换 Activity 并传递一些值。
    • @Stelios 你想在其他活动或课程中进行计算吗?您想将结果从一项活动传递到另一项活动吗?那么你应该使用 Intent 或 SharedPreferences
    • 没错,当我按下一个按钮时,我正在另一个类中进行计算,然后我现在需要在它们完成并通过 getter 访问它们时进行计算。我正在使用一个意图,并且我已经传递了值和所有工作,但是我通过启动线程(类本身)并有一个 while 循环来检查它是否已完成来完成此操作。这使得 hte 应用程序没有响应,但 3-4 秒后它会更改活动并且数据在那里。我只想在后台执行此操作,以免冻结。
    • 我以前使用过 java GUI,我有一个线程正在运行并检查该线程是否完成,所以我在 java 中完成了。
    • 我认为你应该使用类似的东西:isDone = CalcutationClass.doAlotOfCalculations();并将此表达式放入线程中,即我在答案中描述的那个..您不必等待该类完成其计算。否则 AsyncTask 也是个好主意
    【解决方案2】:

    我不明白这对AsyncTask 不起作用。您基本上需要覆盖两个方法 - doInBackground()onPostExecute()。 您可以保证在后台计算完成后,将在 UI 线程上调用 onPostExecute()。您也不必担心如何从另一个线程更新 UI 线程。

    这是一个很好的example

    【讨论】:

    • 所以如果我在 AsyncTask 类中创建类并在 doBackroung() 中调用计算,当我按下按钮时,我应该在 postexecute 中返回什么?因为它不会返回任何东西。我实际上可能会在那里切换活动?如果我想将一些数据传递给主 Activity,我应该在 AsyncTask 中传递类 itelf 并访问/​​编辑这些数据?
    • 仔细阅读文档。在 doInBackground() 中返回的任何内容都会自动作为参数传递给 onPostExecute()。
    【解决方案3】:

    AsyncTask 是解决此问题的正确工具。 AsyncTask 的典型用例是您想在后台做一些小事情,并在任务完成之前、期间和/或之后通过 UI 留下反馈。

    请注意,如果用户退出并重新启动您的 Activity 时,在后台运行可能会给您带来麻烦,因为当 Activity 从屏幕上移除时,后台任务不会结束。

    一个示例活动如下所示。您可以将onPreExecuteonProgress 方法添加到AsynchTask 中,以便在计算之前和期间向用户提供反馈。

        public class CalcActivity extends Activity {
        private Button button;
        private TextView resultView;
    
        public void onCreate() {
            button = (Button) findViewById(R.id.my_button);
            resultView = (TextView) findViewById(R.id.result);
            button.setOnClickListener(new OnClickListener() {
    
                public void onClick(View v) {
                    button.setEnabled(false);
                    AsyncCalculation calc = new AsyncCalculation();
                    calc.execute();
                }
            });
        }
    
        public class AsyncCalculation extends AsyncTask<Void, Void, Integer> {
    
            @Override
            protected Integer doInBackground(Void... params) {
                int result = 0;
                // Do some calculation
                return result;
            }
    
            @Override
            protected void onPostExecute(Integer result) {
                // Set the result, start another activity or do something else
                resultView.setText("The result was " + result);
                button.setEnabled(true);
            }
    
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-19
      • 1970-01-01
      • 2012-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多