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少しが悪い習慣であることを知っています、それは私が片付けることを計画している次のことの1つです)。

どんな助けでも大歓迎です、事前に感謝します。

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 に答える