1

我目前有一个管理一些本地存储的 RSS 提要的 Android 活动。在此活动中,这些提要通过私有类在它们自己的线程中更新。我还尝试包含一个“更新”图标,该图标会RotateAnimation在该线程运行时旋转。

动画自行工作,但在线程运行时不起作用,尽管日志条目表明代码正在执行。我怀疑这是由于线程并不完全安全,并且占用了大部分 CPU 时间。但是我只想知道是否有更好的方法来实现这一点。

updateAllFeeds()通过按下按钮调用该函数。以下是相关代码:

/**
 * Gets the animation properties for the rotation
 */
protected RotateAnimation getRotateAnimation() {
    // Now animate it
    Log.d("RSS Alarm", "Performing animation");
    RotateAnimation anim = new RotateAnimation(359f, 0f, 16f, 21f);
    anim.setInterpolator(new LinearInterpolator());
    anim.setRepeatCount(Animation.INFINITE);
    anim.setDuration(700);
    return anim;
}

/**
 * Animates the refresh icon with a rotate
 */
public void setUpdating() {
    btnRefreshAll.startAnimation(getRotateAnimation());
}

/**
 * Reverts the refresh icon back to a still image
 */
public void stopUpdating() {
    Log.d("RSS Alarm", "Stopping animation");
    btnRefreshAll.setAnimation(null);
    refreshList();
}

/**
 * Updates all RSS feeds in the list
 */
protected void updateAllFeeds() {
    setUpdating();
    Updater updater = new Updater(channels);
    updater.run();

}

/**
 * Class to update RSS feeds in a new thread
 * @author Michael
 *
 */
private class Updater implements Runnable {

    // Mode flags
    public static final int MODE_ONE = 0;
    public static final int MODE_ALL = 1;

    // Class vars
    Channel channel;
    ArrayList<Channel> channelList;
    int mode;

    /**
     * Constructor for singular update
     * @param channel
     */
    public Updater(Channel channel) {
        this.mode = MODE_ONE;
        this.channel = channel;
    }

    /**
     * Constructor for updating multiple feeds at once
     * @param channelList The list of channels to be updated
     */
    public Updater(ArrayList<Channel> channelList) {
        this.mode = MODE_ALL;
        this.channelList = channelList;
    }

    /**
     * Performs all the good stuff
     */
    public void run() {
        // Flag for writing problems
        boolean write_error = false;

        // Check if we have a singular or list
        if(this.mode == MODE_ONE) {
            // Updating one feed only
            int updateStatus = channel.update(getApplicationContext());

            // Check for error
            if(updateStatus == 2) {
                // Error - show dialog
                write_error = true;
            }
        }
        else {
            // Iterate through all feeds
            for(int i = 0; i < this.channelList.size(); i++) {
                // Update this item
                int updateStatus = channelList.get(i).update(getApplicationContext());                
                 if(updateStatus == 2) {
                     // Error - show dialog
                     write_error = true;
                 }
            }
        }

        // If we have an error, show the dialog
        if(write_error) {
            runOnUiThread(new Runnable(){
                public void run() {  
                    showDialog(ERR_SD_READ_ONLY);
                }
             });
        }

        // End updater
        stopUpdating();
    }   // End run()
}   // End class Updater

(我知道这updateStatus == 2是不好的做法,这是我打算整理的接下来的事情之一)。

非常感谢任何帮助,非常感谢提前。

4

3 回答 3

0

在单独的线程中运行可运行的更新程序。进行以下更改。

protected void updateAllFeeds() {
    setUpdating();
    new Thread( new Updater(channels)).start();
}

调用块stopUpdating()runOnUiThread

private class Updater implements Runnable {
    .........
    .........    
    .........
    public void run() {
        .........
        .........

        // End updater
        runOnUiThread(new Runnable(){
                public void run() {  
                     stopUpdating();
                }
             });

    }   // End run()
}   // End class Updater
于 2011-09-03T03:58:36.080 回答
0

将任何影响 UI 的东西移到它自己的Runnable位置,然后用你的按钮发布

btnRefreshAll.post(new StopUpdating());

于 2011-09-03T04:03:35.597 回答
0

我昨晚使用 Android 的AsyncTask类成功地完成了这项工作。实现起来非常容易,但缺点是我必须编写一个类来更新单个提要,另一个类来更新所有提要。这是一次更新所有提要的代码:

private class MassUpdater extends AsyncTask<ArrayList<Channel>, Void, Void> {

    @Override
    protected Void doInBackground(ArrayList<Channel>... channels) {
        ArrayList<Channel> channelList = channels[0];

        // Flag for writing problems
        boolean write_error = false;

            // Iterate through all feeds
            for(int i = 0; i < channelList.size(); i++) {
                // Update this item
                int updateStatus = channelList.get(i).update(getApplicationContext());                
                 if(updateStatus == FileHandler.STATUS_WRITE_ERROR) {
                     // Error - show dialog
                     write_error = true;
                 }
            }


        // If we have an error, show the dialog
        if(write_error) {
            runOnUiThread(new Runnable(){
                public void run() {  
                    showDialog(ERR_SD_READ_ONLY);
                }
             });
        }
        return null;
    }

    protected void onPreExecute() {
        btnRefreshAll.setAnimation(getRotateAnimation());
        btnRefreshAll.invalidate();
        btnRefreshAll.getAnimation().startNow();
    }

    protected void onPostExecute(Void hello) {
        btnRefreshAll.setAnimation(null);
        refreshList();
    }
}

谢谢你们的回答。userSeven7s 的回复也很有意义,所以如果我遇到 AsyncTask 的任何问题,我可以将其用作备份。

于 2011-09-03T11:05:19.323 回答