开发者

Android User Interface Problems

I'm trying to get my user interface to react to events (like button presses, thread completions, etc.) in real time in Android (obviously).

I have a button layout, and one of the buttons is used to copy an unknown number of files from a remote computer using FTP. The FTP part of all this is working very well, but I just cannot find a way to let the user know the state of things:

The states, as I see them are: 1) Selected "Download Files" from "normal" menu. 2) Pressed Confirm (the download process may be quite lengthy and perhaps I don't want to select it by mistake -- however now it's a separate thread so may need to re-think that. 3) Downloading 4) Download complete, restore normal menu

One of the things I hoped would work would be to run the FTP code in a separate thread, and by using the thread.isAlive() construction, wait for the thread to complete and change the display accordingly.

The only thing I haven't been able to do is display that files are downloading. Regardless of what I try, the display jumps from the "Confirm Download" view to the "Normal Menu" view. (Please note, these are not Views as Android defines them in any way.)

Code follows:

Btn.setOnClickListener (new View.OnClickListener()
{
    @Override
    public void onClick (View v)
    {
        hideTempWidgets();
        Btn01.setVisibility (View.GONE);                
        Btn0开发者_如何学Python2.setVisibility (View.GONE);                
        Btn03.setVisibility (View.GONE);                

        verfBtn.setVisibility (View.VISIBLE);
        verfBtn.setText ("Press to Verify");
        verfBtn.setOnClickListener (null);
        verfBtn.setOnClickListener (new View.OnClickListener()
        {
            @Override
            public void onClick (View v)
            {
                runOnUiThread (new Runnable()
                {
                    public void run()
                    {
                        verfBtn.setText ("Downloading...");
                    }
                });

                Thread temp = new Thread (new Runnable()
                {
                    public void run()
                    {
                        try
                        {
                            FileTransfer.getFiles (getAddr().trim());
                        }
                        catch (SQLException e)
                        { }
                    }
                }, "ftp");

                temp.start();
                while (temp.isAlive());                                 

                verfBtn.setVisibility (View.GONE);
                Btn01.setVisibility (View.VISIBLE);
                Btn02.setVisibility (View.VISIBLE);
                Btn03.setVisibility (View.VISIBLE);
                alert (true, VIBE_BLIP);
            }
        });
    }
});

Has anybody faced this and come up with a decent solution??

What's not happening is the setText to "Downloading...", or at least, if it does, not when I want it to and too fast to see... Finally, what's especially frustrating is that that command does work properly when I remove the code to run the FTP thread.

Thanks, R.


This task seems to be perfect for Async Task. Basically Async Task is a Thread with a built in, thread safe component to allow you to publish updates to the UI and notify when the task is complete. A popular idea is to show a spinner or progress bar, until the AsyncTask is complete, then dismiss it.

For more information about Async Task see: http://developer.android.com/reference/android/os/AsyncTask.html


I think your mistake is here:

temp.start(); while (temp.isAlive());

The code inside onClick() is already running on graphical Thread, so there is no need for a runOnUIThread() method and you should not block it with the while() statement, which in fact is blocking the UI thread till the Thread completes.

My advice: you can start an AsyncTask instead and update the UI accordingly.


Since it is a very long file transfer, I'd suggest you consider a service to do the work. If you use thread, user will be bound to keep your application on top of others. Otherwise your thread can be just killed. Worst of all, there is not much you can do to prevent user from going to home screen or other application (some tricks are possible but user will be pissed).

If you use service, you can always use notification to show progress and go back to your application.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜