开发者

In Middle of the process application closes in android?

开发者 https://www.devze.com 2023-03-15 08:14 出处:网络
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 a

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开发者_StackOverflow();

        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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号