用进度条Android

编程入门 行业动态 更新时间:2024-10-14 18:18:15
本文介绍了用进度条Android-状态栏通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我现在有一个状态栏通知,当一个MP3从网址下载的熄灭。它启动精细(文件下载是否正常),但是,进度条不进步。它只是保持不动。我不知道放在哪里code,因此通知是下载的进度。任何想法?

感谢。

下面是我的code的一部分:

公共类DownloadFile扩展的AsyncTask<字符串,整数,字符串> {        @覆盖        保护字符串doInBackground(字符串... URL){            尝试{                URL URL2 =新的URL(sdrUrl);                HttpURLConnection的C =(HttpURLConnection类)url2.openConnection();                c.setRequestMethod(GET);                c.setDoOutput(真);                c.connect();                INT lengthOfFile = c.getContentLength();                字符串PATH = Environment.getExternalStorageDirectory()                        +/下载/;                Log.v(,路径:+路径);                档案文件=新的文件(路径);                file.mkdirs();                。的String [] PATH = url2.getPath()分裂(/);                串MP3 =路径[path.length-1];                串sdrMp3 = mp3.replace(%20,);                文件OUTPUTFILE =新的文件(文件,sdrMp3);                FOS的FileOutputStream =新的FileOutputStream(OUTPUTFILE);                InputStream为= c.getInputStream();                字节[]缓冲区=新的字节[1024];                INT LEN1 = 0;                而((LEN1 = is.​​read(缓冲液))!= - 1){                    publishProgress((int)的(LEN1 * 100 / lengthOfFile));                    fos.write(缓冲液,0,LEN1);                }                fos.close();                is.close();                }赶上(IOException异常五){                       e.printStackTrace();                }            返回null;        }        @覆盖        公共无效onProgressUpdate(整数...值){            super.onProgressUpdate(值);        }    }    公共无效DownloadNotification(){        意向意图=新意图(这一点,BlogActivity.class);        最终的PendingIntent的PendingIntent = PendingIntent.getActivity(getApplicationContext(),0,意向,0);        最后通知的通知=新的通知(R.drawable.sdricontest,下载...,系统               .currentTimeMillis());        notification.flags = notification.flags | Notification.FLAG_ONGOING_EVENT;        notification.contentView =新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,模拟进展);        notification.contentView.setProgressBar(R.id.download_progress,100,进步,FALSE);        getApplicationContext();        最后NotificationManager notificationManager =(NotificationManager)getApplicationContext()。getSystemService(                Context.NOTIFICATION_SERVICE);        notificationManager.notify(42通知);    }}

解决方案

请尝试以下 -

公共类DownloadFile扩展的AsyncTask<字符串,整数,字符串> {    ProgressDialog PD;    公共DownloadFile(){    超();    PD = ProgressDialog.show(背景下,中..,做的东西,真,假);    }    @覆盖    保护字符串doInBackground(字符串... URL){        //东东    }    @覆盖    保护无效onPostExecute(布尔结果){    pd.dismiss();    }}

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); } }

解决方案

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(); } }

更多推荐

用进度条Android

本文发布于:2023-11-27 19:52:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1639376.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:进度条   Android

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!