如何转换使用德precated此方法code? [英] How to convert this code that use deprecated methods?

查看:230
本文介绍了如何转换使用德precated此方法code?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用我的第一次的AsyncTask类作为内部类在我的主要活动类。
这里是code:

I'm using for my first time an AsyncTask class as inner class in my main activity class. Here is the code:

    // Async Task Class
    class fetchdata extends AsyncTask<String, String, String> {

        // Show Progress bar before downloading file
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            // Shows Progress Bar Dialog and then call doInBackground method
            showDialog(progress_bar_type);
        }

        // Download File from Internet
        @Override
        protected String doInBackground(String... f_url) {
            int count;
            try {
                URL url = new URL(f_url[0]);
                URLConnection conection = url.openConnection();
                conection.connect();
                // Get Music file length
                int lenghtOfFile = conection.getContentLength();
                // input stream to read file - with 8k buffer
                InputStream input = new BufferedInputStream(url.openStream(),10*1024);
                // Output stream to write file in SD card
                OutputStream output = new FileOutputStream(Environment.getExternalStorageDirectory().getPath()+"/jai_ho.mp3");
                byte data[] = new byte[1024];
                long total = 0;
                while ((count = input.read(data)) != -1) {
                    total += count;
                    // Publish the progress which triggers onProgressUpdate method
                    publishProgress("" + (int) ((total * 100) / lenghtOfFile));

                    // Write data to file
                    output.write(data, 0, count);
                }
                // Flush output
                output.flush();
                // Close streams
                output.close();
                input.close();
            } catch (Exception e) {
                Log.e("Error: ", e.getMessage());
            }
            return null;
        }

        // While Downloading File
        protected void onProgressUpdate(String... progress) {
            // Set progress percentage
            prgDialog.setProgress(Integer.parseInt(progress[0]));
        }

        // Once File is downloaded
        @Override
        protected void onPostExecute(String file_url) {
            // Dismiss the dialog after the file was downloaded
            dismissDialog(progress_bar_type);
            Toast.makeText(getApplicationContext(), "Download complete", Toast.LENGTH_LONG).show();
            // Play the music
        }
    }

Eclipse的告诉我的ShowDialog和dismissDialog方法去precated。
如何使用DialogFragment类FragmentManager改变我的code?

Eclipse tell me that showDialog and dismissDialog methods are deprecated. How to change my code using DialogFragment class with FragmentManager?

推荐答案

请变化(如参数,返回类型。)当你需要对结构:

Make changes (eg. parameters, return types) as you require to structure :

private class getData extends AsyncTask<Void, Void, Void> {

@Override
protected void onPreExecute() {
    super.onPreExecute();
    // Showing progress dialog
    progressDialog = new ProgressDialog(activityName.this);//getApplicationContext()
    progressDialog.setMessage("Please wait...");
    progressDialog.setCancelable(false);
    progressDialog.show();

}

@Override
protected Void doInBackground(Void... arg0) {   
    // Making a request to url and getting response

    return null;
}

@Override
protected void onPostExecute(Void result) {
    super.onPostExecute(result);
    // Dismiss the progress dialog
    if (progressDialog.isShowing())
        progressDialog.dismiss();
    /**
     * To do code
     * */

    }
}

添加以下行主类启动后:

Add following line after your main class starts:

private ProgressDialog processDialog;

这篇关于如何转换使用德precated此方法code?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆