用进度条Android-状态栏通知 [英] Android- status bar notification with progress bar

查看:153
本文介绍了用进度条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-状态栏通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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