0

画像をダウンロードして電話のSDカードに保存しようとしている非同期タスクを実装しました。OnPostExecute 再度非同期タスクを呼び出します。これは常に継続します。しかし、1 時間ほどすると、アプリは ANR と言い始めます。このシーンをどのように処理すればよいでしょうか。

    public class DownloadSavePics extends AsyncTask<Void, Void, String> {
        Context context;
        String Tag = "DownloadSavePics";
        String res;
        QVSEngine qvsEngine;
        String IID; 
        String url1 = "";
        String deviceId ="";
        String FileName ="";

        /*
         * Pass all data required to log in and handle the result here.
         */

        public DownloadSavePics(Context context,String url1,String deviceId) {
            this.context = context;
            this.deviceId = deviceId;
            this.url1 = url1;
            qvsEngine = new QVSEngine(context,null);        
        }

        @Override
        protected void onPreExecute() { 
            Log.i(Tag, "inside pre execute");
        }

        /*
         * Do all the network IO and time-consuming parsing in here, this method
         * will be invoked in its own thread, not blocking the UI.
         */

        protected String doInBackground(Void... params) {
            /*
             * the background thread calls the method CallService 
             * 
             */
            if (qvsEngine.checkInternetConnection()){   
                CallService();  
            }
            return FileName;        
        }

        public void CallService (){
            /*
             *  downloading the file
             */
            try {
                if(qvsEngine.checkInternetConnection()){
                    /*
                     * requested for connection so set the led1 as amber 
                     */
                    try {
                        URL imgURL = new URL(url1);
                        Log.e(Tag,"Downloading from hotspot "+url1);
                        URLConnection ucon = imgURL.openConnection();
                        InputStream is = ucon.getInputStream();
                        BufferedInputStream bis = new BufferedInputStream(is);
                        ByteArrayBuffer baf = new ByteArrayBuffer(50);
                        int current = 0;                    
                        while ((current = bis.read()) != -1) {
                            baf.append((byte) current);
                        }
/*saving the file in the sdcard
*/                                  
                        String FileName = deviceId.trim()+".jpg";                   
                        String path = Environment.getExternalStorageDirectory().toString()+"/school/snaps/"+FileName;;
                        FileOutputStream fos = new FileOutputStream(path);
                        fos.write(baf.toByteArray());
                        fos.close();                                    
                    }catch(Exception e){
                        Log.e(Tag,""+e);
                    } 
                }
            }catch(Exception e){
                Log.e(Tag,""+e);
            }
        }


        @Override
        protected void onPostExecute(String result) {
            try {
                qvsEngine.database.RunQuery("insert into webcam values (null,'"+FileName+"','0')");
                Log.e(Tag,"webcame table has :  "+ qvsEngine.RunQuery("select count(imgid) from webcam where path ='"+FileName+"'") );      
            }catch(Exception e){
                Log.e(Tag,""+e);
            }   
            new DownloadSavePics(context,url1,deviceId).execute();
        }

    }
4

2 に答える 2

2

はい、「アプリケーションが応答していません」というメッセージが表示されます。

あなたが呼んでいる

 new DownloadSavePics(context,url1,deviceId).execute();

inonPostExecuteで、再度実行し、DownloadSavePicsonPostExecute に到達すると、再度呼び出しを受ける....ということで終わりはありません。

連続通話DownlaodSavePics通話の結果ANR

于 2013-04-12T12:13:00.267 に答える
0

runOnUIThread() を介して Runnable から新しいインスタンスを実行してみてください。それでも解決しない場合は、ログを投稿してください。

アイデアは次のとおりです。おそらく、タスクがデータのダウンロードに失敗し (たとえば、WiFi が壊れた)、すぐ実行されるタスクの無限ループを生成します。(ワーカースレッドに) sleep(500) を追加してみて、これが役立つかどうかを確認してください。

于 2013-04-12T12:19:21.710 に答える