1

ActionBarSherlock の問題により、マニフェストをターゲット API 16 に変更しました。それ以降、現在再生中の曲をチェックするハンドラが機能しなくなりました。以下でマークした行で NetworkOnMainThreadException をスローします。

私は何を間違っていますか?マルチスレッドが正しく設定されていると思いました。

これが私のコードです:

    handler = new Handler();

    updateSongTask = new Runnable() {
        public void run() {
            Log.d("asdf", "scraper started");
            Scraper scraper = new ShoutCastScraper(); // THIS LINE throws the exception
            List<Stream> streams;
            try {
                streams = scraper.scrape(new URI(url));
                for (Stream stream : streams) {                     
                    Intent songIntent = new Intent(CUSTOM_INTENT);

                    String[] songAndArtist = songAndArtist(stream.getCurrentSong());
                    songIntent.putExtra("song", songAndArtist[0]);
                    songIntent.putExtra("artist", songAndArtist[1]);
                    songIntent.putExtra("stationurl", url);
                    sendBroadcast(songIntent);
                    Log.d("asdf", "should send broadcast" );

                }
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            handler.postDelayed(this, 5000);
        }
    };

    handler.postDelayed(updateSongTask, 0);
4

2 に答える 2

5

postDelayed()Runnable遅延後にメインアプリケーションスレッドでを実行するようにAndroidに指示します。Runnableバックグラウンドスレッドでは実行されません。

于 2012-07-03T20:22:42.763 に答える
0

CommonsWareは正しかった。ハンドラー内にASyncTaskを配置し、すべての曲の更新(ワーカー)コードをdoInBackground()メソッドに移動しました。すべてのバージョンでうまく機能し、ネットワーク例外はありません!

于 2012-07-03T21:03:12.847 に答える