0

I have one activity which download data from the web. In middle of the downloading process application closes if I start downloading process more than one time. I am using AsyncTask to download data and Runnable to get the steps completed and. Here is my code. Do I have to remove callback for handler at finish of the task? If i put remove callback in pause or destroy method it gives force close error.

mBackgroundHandler = new Handler();
mRunnable = new Runnable()
{
    public void run()
    {
        mCounterProgress = KamaApplication.getStep();
        Log.v(TAG, "Progress: " + mCounterProgress);
        mRefreshProgress.setProgress(mCounterProgress);
        mRefreshProgress.invalidate();

        if(KamaApplication.getTotalStep() == 100 && KamaApplication.getStep() == 99)
        {
            mStop = true;
        }

        if(!mStop)
        {
            mBackgroundHandler.postDelayed(this, 1000);
        }
    }
};
mBackgroundHandler.postDelayed(mRunnable, 1000);

Here is my AsyncTask

private class DownloadFilesTask extends AsyncTask<Void, Integer, Void> 
{
    protected Void doInBackground(Void... params) 
    {
        try 
        {
            StepsGeneratorInputJsonData stepsGeneratorInputJsonData = new StepsGeneratorInputJsonData();
            stepsGeneratorInputJsonData.provider = "Android";
            stepsGeneratorInputJsonData.identification = "Vogue";
            stepsGeneratorInputJsonData.password = "abcd";
            mResult = createAndExecuteStepsGenerator(stepsGeneratorInputJsonData);
            JSONObject mNewJSONObject = new JSONObject(mResult);
            String mEndDate = mNewJSONObject.getString("endDate");
            if(mEndDate.equals("null"))
            {
                mRefreshSuccess = false;
            }
            else
            {
                mRefreshSuccess = true;
            }

        } 
        catch (Exception eException) 
        {
            Log.v(TAG, "HotTestGettingError: " + eException.toString());
            mRefreshSuccess = false;
        }
        return null;
   }
   protected void onProgressUpdate(Integer... progress) 
   { }
   protected void onPostExecute(Void result) 
   {
        if(mRefreshSuccess)
        {
            insertData();
        }
        else
        {
            Message mMessage = new  Message();
            mMessage.what = 0;
            mHandler.sendMessage(mMessage);
        }
        mBackgroundHandler.removeCallbacks(mRunnable);
        updateDisplay();
    }
}

And I saw the logcat and got this :

06-25 12:08:28.007: INFO/WindowManager(128): WIN DEATH: Window{408fc1d8 com.mygosoftware.kama/com.mygosoftware.kama.Usage paused=false}
06-25 12:08:28.011: INFO/ActivityManager(128): Process com.mygosoftware.kama (pid 17691) has died.
06-25 12:08:28.078: INFO/ActivityManager(128): Start proc com.sec.android.app.twlauncher for activity com.sec.android.app.twlauncher/.Launcher: pid=17817 uid=10015 gids={3003, 1015, 3002}
06-25 12:08:28.078: INFO/ActivityManager(128): Low Memory: No more background processes.
06-25 12:08:28.148: INFO/Zygote(17817): Zygote: pid 17817 has CALL PRIVILEGED permission, then set capability for CAP_SYS_ADMIN (21)
06-25 12:08:28.312: INFO/ActivityThread(17817): Pub com.sec.android.app.twlauncher.settings: com.sec.android.app.twlauncher.LauncherProvider
06-25 12:08:28.445: INFO/Launcher(17817): onCreate():  product model family:S1 product model : GT-I9000

06-24 16:47:38.269: ERROR/InputDispatcher(128): channel '40823250 com.mygosoftware.kama/com.mygosoftware.kama.Usage (server)' ~ Consumer closed input channel or an error occurred.  events=0x8
06-24 16:47:38.269: ERROR/InputDispatcher(128): channel '40823250 com.mygosoftware.kama/com.mygosoftware.kama.Usage (server)' ~ Channel is unrecoverably broken and will be disposed!
06-24 16:47:38.304: INFO/ActivityManager(128): Process com.mygosoftware.kama (pid 7637) has died.

I have put in AndroidManifest file

<activity android:name=".Usage" android:screenOrientation="portrait"
        android:clearTaskOnLaunch="true">

and also set

 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

and also put onPause() method in my activity.

Moreover, I have referred this link Activity restarts on Force Close. Mine is very similar problem. I have done everything what they written in answer but still getting the error.

What should I do to remove this error?

Thanks.

Community
  • 1
  • 1

1 Answers1

0
private void startDownload() {
        String url = GlobalVariable.Getstr();
        new DownloadFileAsync().execute(url);

    }
class DownloadFileAsync extends AsyncTask<String, String, String> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            showDialog(DIALOG_DOWNLOAD_PROGRESS);
        }

        @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case DIALOG_DOWNLOAD_PROGRESS:
            mProgressDialog = new ProgressDialog(this);

//          mProgressDialog.setIcon(R.drawable.icon);
            mProgressDialog.setIcon(R.drawable.tab_icon_pitchforkfm);
            mProgressDialog.setTitle("Downloading file... "
                    + fileName.toString());

            mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            mProgressDialog.setCancelable(true);
            mProgressDialog.show();
            return mProgressDialog;
        default:
            return null;
        }
    }   

    @Override
        protected String doInBackground(String... arg0) {

            int len1 = 0;
            int count;
            try {
                URL url = new URL(GlobalVariable.Getstr());
                HttpURLConnection c = (HttpURLConnection) url.openConnection();
                c.setRequestMethod("GET");
                c.setDoOutput(true);
                c.connect();
                int lenghtOfFile = c.getContentLength();
                Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile);
                String PATH = Environment.getExternalStorageDirectory()
                        + "/download/";
                Log.v(LOG_TAG, "PATH: " + PATH);
                File file = new File(PATH);
                file.mkdirs();

                 String fileName = "workTest.mp3";


                File outputFile = new File(file, fileName);

                FileOutputStream fos = new FileOutputStream(outputFile);

                InputStream is = c.getInputStream();

                byte[] buffer = new byte[1024];
                long total = 0;

                while ((count = is.read(buffer)) != -1) {
                    total += count;
                    publishProgress("" + (int) ((total * 100) / lenghtOfFile));
                    // fos.write(buffer, 0, len1);
                    fos.write(buffer, 0, count);
                }
                fos.close();
                is.close();

            } catch (IOException e) {
                Log.d(LOG_TAG, "Error: " + e);
            }
            return null;
        }

        protected void onProgressUpdate(String... progress) {
            Log.d("ANDRO_ASYNC", progress[0]);

            mProgressDialog.setProgress(Integer.parseInt(progress[0]));
        }
        protected void onPostExecute(String unused) {
            dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
            //refreshList();
            Toast.makeText(
                    FindFilesByType.this,
                    "Downloading of " + fileName
                            + " complete.", Toast.LENGTH_LONG).show();
            try{
            refreshList();
            } catch (Exception e) {
                Toast.makeText(FindFilesByType.this, "ERROR " + e.toString(),
                        Toast.LENGTH_LONG).show();
            }

above is my entire code.. this code is downloading a specified mp3 file from the URL.. and save it to the SDCARD.. and showing me the progressbar also depending upone the downloading has completed..hope this useful for you.. still getting and problem the let me know Happy coding:):) Pragna

Android
  • 8,995
  • 9
  • 67
  • 108
  • you have to call this method just using startDownload(); – Android Jun 24 '11 at 12:26
  • Hi Pragna, Downloading is not my problem. It downloads data successfully but againg when I start with different account, application closes in middle of the process without any error but if I look at the logcat I got what I put in my question. Thanks. –  Jun 24 '11 at 12:28
  • Hi Pragna, I am calling this method with another method like you have used startDownload(). Thanks. –  Jun 24 '11 at 12:29
  • @Pragna I have added two links which has very similar problem. I have done what they written but no fruitful result. Thanks. –  Jun 24 '11 at 12:35
  • you want that in middle of image(in blue portion? 20% and 0%) – Android Jun 30 '11 at 05:23
  • @Pragna, No not in blue portion but check the bottom one .I want it in middle of the image at bottombar so here i can not use the progressbar which you used in your program, right? Thanks. –  Jun 30 '11 at 05:27
  • @Pragna, I ll check on that. Thanks. –  Jun 30 '11 at 05:53
  • @Pragna, I have checked the example but problem is that one method run my downloading data process while the second method get the steps (please check my code above) and in this example in onProgressUpdate I can not get the steps executing while downloading the data because we need to count manually the progress update and then we have to pass the counted integer to the publishProgress method but I do not want to show how much data is downloaded. I want to show how many steps executed while downloading data. So this is not fit in my case. Thanks. –  Jun 30 '11 at 07:25
  • I have updated my code according to yours now it works fine. Thanks for the help. –  Aug 15 '11 at 12:17