Android - 下载文件+状态栏通知减慢手机 [英] Android- download file + status bar notification slowing down phone

查看:175
本文介绍了Android - 下载文件+状态栏通知减慢手机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个从服务器下载mp3的 asynctask 。当用户开始下载时,会创建状态栏通知。这将显示实时下载的进度。我唯一关心的是手机减速几乎太多了。是否有任何方法可以延迟显示的进度或使我的代码更快的方法?谢谢。

I currently have an asynctask which downloads a mp3 from a server. When the user starts to download it, a status bar notification is created. This displays the progress of the download in real time. My only concern is that the phone slows down almost too much. Is there any way to delay the progress displayed or a way to make my code faster? Thanks.

代码如下:

public class DownloadFile extends AsyncTask<String, String, String> {
        CharSequence contentText;
        Context context;
        CharSequence contentTitle;
        PendingIntent contentIntent;
        int HELLO_ID = 1;
        long time;
        int icon;
        CharSequence tickerText;
        File file;

        public void downloadNotification(){
            String ns = Context.NOTIFICATION_SERVICE;
            notificationManager = (NotificationManager) getSystemService(ns);

            icon = R.drawable.sdricontest;
            //the text that appears first on the status bar
            tickerText = "Downloading...";
            time = System.currentTimeMillis();

            notification = new Notification(icon, tickerText, time);

            context = getApplicationContext();
            //the bold font
            contentTitle = "Your download is in progress";
            //the text that needs to change
            contentText = "0% complete";
            Intent notificationIntent = new Intent(Intent.ACTION_VIEW);
            notificationIntent.setType("audio/*");
            contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

            notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
            notificationManager.notify(HELLO_ID, notification);

        }


        @Override
        protected void onPreExecute() {
            //execute the status bar notification
            downloadNotification();

            super.onPreExecute();
        }

        @Override
        protected String doInBackground(String... url) {
            int count;
            try {
                URL url2 = new URL(sdrUrl);
                HttpURLConnection connection = (HttpURLConnection) url2.openConnection();
                connection.setRequestMethod("GET");
                connection.setDoOutput(true);
                connection.connect();

                int lengthOfFile = connection.getContentLength();

                //make the stop drop rave folder
                File sdrFolder = new File(Environment.getExternalStorageDirectory() + "/StopDropRave");
                boolean success = false;

                if(!sdrFolder.exists()){
                    success = sdrFolder.mkdir();
                }      
                if (!success) { 
                    String PATH = Environment.getExternalStorageDirectory()
                            + "/StopDropRave/";
                    file = new File(PATH);
                    file.mkdirs();

                }
                else 
                {
                    String PATH = Environment.getExternalStorageDirectory()
                            + "/StopDropRave/";
                    file = new File(PATH);
                    file.mkdirs();
                }

                String[] path = url2.getPath().split("/");
                String mp3 = path[path.length - 1];
                String mp31 = mp3.replace("%20", " ");
                String sdrMp3 = mp31.replace("%28", "(");
                String sdrMp31 = sdrMp3.replace("%29", ")");
                String sdrMp32 = sdrMp31.replace("%27", "'");

                File outputFile = new File(file, sdrMp32);
                FileOutputStream fos = new FileOutputStream(outputFile);

                InputStream input = connection.getInputStream();

                byte[] data = new byte[1024];
                long total = 0;
                while ((count = input.read(data)) != -1) {
                    total += count;
                    publishProgress(""+(int) (total * 100 / lengthOfFile));
                    fos.write(data, 0, count);
                }
                fos.close();
                input.close();

            } catch (IllegalArgumentException e) 
            { e.printStackTrace();
            }catch (IllegalStateException e) { 
                e.printStackTrace();
            }catch (IOException e) {
            e.printStackTrace();}

            return null;
        }

        @Override
        public void onProgressUpdate(String... progress) {
            contentText =  Integer.parseInt(progress[0]) + "% complete";
            notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
            notificationManager.notify(HELLO_ID, notification);
            super.onProgressUpdate(progress);
        }


推荐答案

我看到类似的结果,你需要不频繁地推送更新通知,我更改了我的更新只能更新几秒钟。 (例如,onProgressUpdate会记录上次调用通知的时间,并且只有在超过上一次通话的100ms时才调用通知,或者如果您处于最大值。

I saw similar results, you need to not push the update the notification so often, i changed mine to update only update a few times a second. (e.g. in onProgressUpdate keep track of the last time you called notify, and only call notify if you're past 100ms of the previous call, or if you're at the max value.

这篇关于Android - 下载文件+状态栏通知减慢手机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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