1

所以我有主 UI 线程,并且有一个需要相当长的时间的函数。所以我想在那段时间在屏幕上向用户展示一些东西,但我不想将我的函数移动到线程中,因为它需要一些 GUI 通信。

所以我需要的是这个

 Message msg;
 msg.wait("wait dude");
 //my code
 msg.dismis_wait();

有任何想法吗?

我尝试将我的代码放入线程中,但有一个问题是在代码结束时我需要更改我的视图,而我无法从线程中做到这一点。

4

5 回答 5

1

Message is not the one you want to use.

You should try with Toast Notifications.

Below an example from the documentation:

Context context = getApplicationContext();
CharSequence text = "Hello toast!";
int duration = Toast.LENGTH_SHORT;

Toast toast = Toast.makeText(context, text, duration);
toast.show();
于 2012-09-24T10:26:07.463 回答
1

您可以Toast像建议的其他答案一样使用。

Thread如果您有长时间运行的代码,您也可以使用单独的,并使用Handler向主线程发送消息以在屏幕上进行更改。

您也可以使用AsyncTask,这是一个 Android 特殊线程,可让您在运行期间对视图进行更改,这是在doInBackground方法中完成的。

于 2012-09-24T10:39:57.297 回答
0

If you want a toast message you could do something like this:

for (int i=0; i < 2; i++) { 
    Toast.makeText(yourclass.this, " Here you message ", Toast.LENGTH_LONG).show();
}

For toast you can only choose to display it long or short but cant define a time. Im displaying here the same toast message twice and with that doubling the time i would like to show it.

Put your entire code inside a scheduleTaskExecutor. That one runs code in the background line by line. so you can show your toast message in the beginning. Here an example:

First declare

private ScheduledExecutorService scheduleTaskExecutor;



    scheduleTaskExecutor= Executors.newScheduledThreadPool(5);

    // This schedule a task to run every 10 seconds:

    scheduleTaskExecutor.scheduleAtFixedRate(new Runnable() {
      public void run() {

            for (int i=0; i < 2; i++){ Toast.makeText(yourclass.this, " Here you message ", Toast.LENGTH_LONG).show();}

            try {                 

            // your code here                       


            } catch (IOException e) {                          
               e.printStackTrace();                           
            }                                     

     // If you need update UI, simply do this:
     //   runOnUiThread(new Runnable() {
      //    public void run() {
            // update your UI component here.
        //    myTextView.setText("refreshed");
         // }
      //  });
      }
    }, 0, 10, TimeUnit.SECONDS);

you can stop the thread with:

 scheduleTaskExecutor.shutdownNow();

You can also start it with a delay or run it only once or in other ways. Check the documentation on: https://developer.android.com/reference/java/util/concurrent/ScheduledExecutorService.html

于 2012-09-24T10:26:47.110 回答
0

@gabrjan:Ofir A为您的查询提出了正确的解决方案。永远不要在主线程中运行 long/cpu 密集型操作。使用 AsyncTask/Thread 执行您的后端操作并使用处理程序实例发布 ui 更新。你也可以使用 runOnUiThread 来更新你的 UI。

 runOnUiThread(new Runnable() {
                public void run() {

                      HERE U CAN WRITE UR UI UPDATION CODE.
                }
            });
于 2012-09-24T10:52:40.220 回答
0

尝试使用倒数计时器。这是课程:-

public class MyCount extends CountDownTimer{
    public MyCount(long millisInFuture, long countDownInterval) {
        super(millisInFuture, countDownInterval);
        }

    @Override
    public void onFinish() {
 // Dismiss message
     }

    @Override
    public void onTick(long millisUntilFinished) {

    }
}

并在 onCreate 或您要显示消息的地方使用它。

MyCount counter = new MyCount(15000,1000);
    counter.start();
// Show message

启动 Timer 后显示您的消息 onFinish 的 CountDownTimer 关闭消息。您可以在对象创建时更改这些值。第一个值即“15000”是以毫秒为单位的总时间,第二个值即“1000”是以毫秒为单位的每次滴答时间。

希望这会对你有所帮助。

于 2012-09-24T10:49:18.180 回答