0

我正在运行一个后台线程。后台线程成功执行后,我想在 UI 上向用户显示一些警报消息。

4

4 回答 4

2

if you worked with AsynTask then you can show it in onPostExecute(). http://www.mysamplecode.com/2011/09/android-asynctask-httpclient-with.html

AlertDialog alertDialog = new AlertDialog.Builder(
                        AlertDialogActivity.this).create();


    // Setting Dialog Title
    alertDialog.setTitle("Alert Dialog");

    // Setting Dialog Message
    alertDialog.setMessage("Welcome to AndroidHive.info");

    // Setting OK Button
    alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
            // Write your code here to execute after dialog closed
            Toast.makeText(getApplicationContext(), "You clicked on OK", Toast.LENGTH_SHORT).show();
            }
    });

    // Showing Alert Message
    alertDialog.show();

for more help with alert see http://www.androidhive.info/2011/09/how-to-show-alert-dialog-in-android/

于 2012-04-17T04:29:11.547 回答
0

If you are using AsyncTask, then you could write the code for displaying the message (maybe a Toast), in onPostExecute().

于 2012-04-17T04:29:41.947 回答
0

将后台线程与 UI 连接并非不可能。在处理程序的帮助下,您可以发送消息。通过检查消息,您可以显示警报消息。我认为这段代码会对您有所帮助。

Thread animator = new Thread() {
        public void run() {
            int i = 0;
            try {
                sleep(4000);
                while (i < 4) {
                    sleep(50);
                    handler.sendMessage(handler.obtainMessage(i));
                    i++;
                }
            } catch (Exception e) {

            }
        }
    };
    animator.start();
    handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0) {
                animatedimage.setImageResource(R.drawable.sub1);
            } else if (msg.what == 1) {
                animatedimage.setImageResource(R.drawable.sub2);
            } else if (msg.what == 2) {
                animatedimage.setImageResource(R.drawable.sub3);
            } else if (msg.what == 3) {
                animatedimage.setImageResource(R.drawable.sub4);
            }
        }

    };

如果您使用的是 Assync tasy,您可以在

onPostExecute()
于 2012-04-17T04:32:09.003 回答
0

启动一个异步线程。异步线程为您提供了三种方法:OnPreExecute()、doInBackground() 和 onPostExecute()。

第一个和最后一个方法是在UI线程上调用的,所以一旦在doInBackground中操作

于 2012-04-17T04:32:44.167 回答