Android-带有进度条的状态栏通知

发布于 2024-11-27 03:31:36 字数 2993 浏览 0 评论 0原文

我目前有一个状态栏通知,当从 URL 下载 mp3 时,该通知会关闭。它启动良好(并且文件下载良好),但是进度条没有进度。它只是保持静止。我不确定将代码放在哪里,以便它通知下载进度。有什么想法吗?

谢谢。

这是我的代码的一部分:

public class DownloadFile extends AsyncTask<String, Integer, String>{
        @Override
        protected String doInBackground(String... url) {
            try {
                URL url2 = new URL(sdrUrl);
                HttpURLConnection c = (HttpURLConnection) url2.openConnection();
                c.setRequestMethod("GET");
                c.setDoOutput(true);
                c.connect();

                int lengthOfFile = c.getContentLength();

                String PATH = Environment.getExternalStorageDirectory()
                        + "/download/";
                Log.v("", "PATH: " + PATH);
                File file = new File(PATH);
                file.mkdirs();

                String [] path = url2.getPath().split("/");
                String mp3 = path [path.length-1];
                String sdrMp3 = mp3.replace("%20", "");

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

                InputStream is = c.getInputStream();

                byte[] buffer = new byte[1024];
                int len1 = 0;
                while ((len1 = is.read(buffer)) != -1) {
                    publishProgress((int)(len1*100/lengthOfFile));
                    fos.write(buffer, 0, len1);
                }
                fos.close();
                is.close();

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


            return null;
        }

        @Override
        public void onProgressUpdate(Integer... values) {


            super.onProgressUpdate(values);
        }



    }

    public void DownloadNotification(){

        Intent intent = new Intent(this, BlogActivity.class);
        final PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);

        final Notification notification = new Notification(R.drawable.sdricontest, "Downloading...", System
               .currentTimeMillis());
        notification.flags = notification.flags | Notification.FLAG_ONGOING_EVENT;
        notification.contentView = new RemoteViews(getApplicationContext().getPackageName(), R.layout.notification_layout);
        notification.contentIntent = pendingIntent;
        notification.contentView.setImageViewResource(R.id.download_icon, R.drawable.sdricontest);
        notification.contentView.setTextViewText(R.id.download_text, "simulation in progress");
        notification.contentView.setProgressBar(R.id.download_progress, 100, progress, false);

        getApplicationContext();
        final NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(
                Context.NOTIFICATION_SERVICE);
        notificationManager.notify(42, notification);
    }

}

I currently have a status bar notification that goes off when an mp3 is downloaded from a url. It launches fine (and the file does download fine), however, the progress bar doesn't progress. It simply stays still. I'm not sure where to put the code so it notify's the progress of the download. Any ideas?

Thanks.

Here is part of my code:

public class DownloadFile extends AsyncTask<String, Integer, String>{
        @Override
        protected String doInBackground(String... url) {
            try {
                URL url2 = new URL(sdrUrl);
                HttpURLConnection c = (HttpURLConnection) url2.openConnection();
                c.setRequestMethod("GET");
                c.setDoOutput(true);
                c.connect();

                int lengthOfFile = c.getContentLength();

                String PATH = Environment.getExternalStorageDirectory()
                        + "/download/";
                Log.v("", "PATH: " + PATH);
                File file = new File(PATH);
                file.mkdirs();

                String [] path = url2.getPath().split("/");
                String mp3 = path [path.length-1];
                String sdrMp3 = mp3.replace("%20", "");

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

                InputStream is = c.getInputStream();

                byte[] buffer = new byte[1024];
                int len1 = 0;
                while ((len1 = is.read(buffer)) != -1) {
                    publishProgress((int)(len1*100/lengthOfFile));
                    fos.write(buffer, 0, len1);
                }
                fos.close();
                is.close();

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


            return null;
        }

        @Override
        public void onProgressUpdate(Integer... values) {


            super.onProgressUpdate(values);
        }



    }

    public void DownloadNotification(){

        Intent intent = new Intent(this, BlogActivity.class);
        final PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);

        final Notification notification = new Notification(R.drawable.sdricontest, "Downloading...", System
               .currentTimeMillis());
        notification.flags = notification.flags | Notification.FLAG_ONGOING_EVENT;
        notification.contentView = new RemoteViews(getApplicationContext().getPackageName(), R.layout.notification_layout);
        notification.contentIntent = pendingIntent;
        notification.contentView.setImageViewResource(R.id.download_icon, R.drawable.sdricontest);
        notification.contentView.setTextViewText(R.id.download_text, "simulation in progress");
        notification.contentView.setProgressBar(R.id.download_progress, 100, progress, false);

        getApplicationContext();
        final NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(
                Context.NOTIFICATION_SERVICE);
        notificationManager.notify(42, notification);
    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

昔梦 2024-12-04 03:31:36

http://developer.android.com/training/notify-user/display -progress.html

这将一直为您工作,直到应用程序在前台运行。如果应用程序停止,进度条就会卡住。最好使用服务下载

http://developer.android.com/training/notify-user/display-progress.html

This will work for you until the app is running on foreground. If the app stops the progress bar gets stuck. Its better to download using a service

雪若未夕 2024-12-04 03:31:36

尝试以下操作 -

    public class DownloadFile extends AsyncTask<String, Integer, String>{
    ProgressDialog pd;

    public DownloadFile() {
    super();
    pd = ProgressDialog.show(context, "Loading..", "Doing Stuff", true,false);
    }

    @Override
    protected String doInBackground(String... url) {
        //stuff
    }


    @Override
    protected void onPostExecute (Boolean result){

    pd.dismiss();

    }


}

Try the following -

    public class DownloadFile extends AsyncTask<String, Integer, String>{
    ProgressDialog pd;

    public DownloadFile() {
    super();
    pd = ProgressDialog.show(context, "Loading..", "Doing Stuff", true,false);
    }

    @Override
    protected String doInBackground(String... url) {
        //stuff
    }


    @Override
    protected void onPostExecute (Boolean result){

    pd.dismiss();

    }


}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文