Android:运行线程阻止动画启动

Android:运行线程阻止动画启动,android,multithreading,animation,rotateanimation,Android,Multithreading,Animation,Rotateanimation,我目前有一个Android活动,它管理一些本地存储的RSS提要。在这个活动中,这些提要通过私有类在它们自己的线程中更新。我还试图包括一个“更新”图标,该图标在该线程运行时随rotateAimation旋转 动画本身工作,但在线程运行时不工作,尽管日志条目说明代码正在执行。我怀疑这是由于线程不是完全安全的,并且占用了大部分CPU时间。然而,我只是想知道是否有更好的方法来实现这一点 按下按钮可调用函数updatealFeeds()。以下是相关代码: /** * Gets the animation

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

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

按下按钮可调用函数
updatealFeeds()
。以下是相关代码:

/**
 * 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
/**
*获取旋转的动画属性
*/
受保护的RotateAnimation getRotateAnimation(){
//现在设置动画
Log.d(“RSS警报”、“执行动画”);
旋转动画=新的旋转动画(359f、0f、16f、21f);
anim.setInterpolator(新的LinearInterpolator());
anim.setRepeatCount(Animation.INFINITE);
动画设定持续时间(700);
返回动画;
}
/**
*使用旋转设置刷新图标的动画
*/
公共约会(){
btnRefreshAll.startAnimation(getRotateAnimation());
}
/**
*将刷新图标恢复为静止图像
*/
公共无效停止更新(){
Log.d(“RSS警报”、“停止动画”);
btnRefreshAll.setAnimation(空);
刷新列表();
}
/**
*更新列表中的所有RSS源
*/
受保护的void updatealFeeds(){
设置日期();
Updater Updater=新的更新程序(通道);
updater.run();
}
/**
*类以在新线程中更新RSS源
*@作者迈克尔
*
*/
私有类更新程序实现Runnable{
//模式标志
公共静态最终整数模式_ONE=0;
公共静态最终整数模式_ALL=1;
//类变量
渠道;
ArrayList频道列表;
int模式;
/**
*奇异更新的构造函数
*@param通道
*/
公共更新程序(频道){
this.mode=mode_ONE;
this.channel=channel;
}
/**
*一次更新多个提要的构造函数
*@param channelList要更新的频道列表
*/
公共更新程序(ArrayList通道列表){
this.mode=mode\u ALL;
this.channelList=channelList;
}
/**
*表演所有的好东西
*/
公开募捐{
//书写问题的标志
布尔写入错误=false;
//检查我们是否有单数或单数列表
if(this.mode==mode_ONE){
//仅更新一个提要
int updateStatus=channel.update(getApplicationContext());
//检查错误
如果(updateStatus==2){
//错误显示对话框
write_error=true;
}
}
否则{
//遍历所有提要
对于(int i=0;i
(我知道
updateStatus==2
位是不好的做法,这是我计划整理的下一件事之一)


非常感谢您提供的任何帮助。请在单独的线程中运行更新程序runnable。执行以下更改

protected void updateAllFeeds() {
    setUpdating();
    new Thread( new Updater(channels)).start();
}
runOnUiThread
块中调用
stopUpdate()

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

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

    }   // End run()
}   // End class Updater

将任何影响UI的内容移入自己的
Runnable
,然后用按钮发布


btnRefreshAll.post(新的停止更新())

我昨晚使用Android的类成功地实现了这一点。实现起来非常简单,但缺点是我必须编写一个类来更新单个提要,另一个类用于更新所有提要。下面是一次更新所有提要的代码:

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();
    }
}
私有类MassUpdater扩展异步任务{
@凌驾
受保护的Void doInBackground(ArrayList…通道){
ArrayList channelList=通道[0];
//书写问题的标志
布尔写入错误=false;
//遍历所有提要
对于(int i=0;i

谢谢你们的回答,伙计们。userSeven7s的回复也很有意义,因此,如果我在AsyncTask中遇到任何问题,我可以使用它作为备份。

只是为了澄清:您不能尝试从主应用程序线程以外的线程更新UI<有限公司