在通知区域进度条不打烊,不启动第二进度条 [英] progress bar in Notification area is not closing and not starting the second progress bar

查看:180
本文介绍了在通知区域进度条不打烊,不启动第二进度条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序,我有4个按钮,当用户点击任何按钮启动下载文件,进度栏通知区域显示得到。下载和进度条工作正常,但我有以下两个问题。


  1. 当下载完成吧是没有得到关闭的进步,但它仍然在通知区域

  2. 正如我上面所说的,我有4个按钮,并点击第一个按钮,当下载器启动,其他三个按钮点击立即下载时不会发生。我想先下载完成后,它可能会启动。但没有任何反应。如何显示所有的进度条,当所有按钮点击

以下是我的code(这里我仅添加了2个按钮),请帮助我

  B1 =(按钮)findViewById(R.id.button1);
        b1.setOnClickListener(新View.OnClickListener()
        {
            @覆盖
            公共无效的onClick(视图v)
            {
                I = 1;
                意向意图=新意图(NotificationProgressTestActivity.this,UploadService.class);
                startService(意向);
            }
        });        B2 =(按钮)findViewById(R.id.button2);
        b2.setOnClickListener(新View.OnClickListener()
        {
            @覆盖
            公共无效的onClick(视图v)
            {
                我= 2;
                意向意图=新意图(NotificationProgressTestActivity.this,UploadService.class);
                startService(意向);
            }
        });

下一步以下是我的 Uplaod Service.class

 公共类UploadService扩展IntentService
{
    私人NotificationManager notificationManager;
    私人的通知的通知;
    私人INT进度= 10;
    私有静态字符串文件名=文件夹/;
    私有静态URL网址;
    公共UploadService(字符串名称)
    {
        超(名);
    }
    公共UploadService()
    {
        超级(UploadService);
    }
    @覆盖
    保护无效onHandleIntent(意向意图)
    {
        。notificationManager =(NotificationManager)getApplicationContext()getSystemService(getApplicationContext()NOTIFICATION_SERVICE);
        的PendingIntent contentIntent = PendingIntent.getActivity(这一点,0,意向,0);
        通知=新的通知(R.drawable.icon,上传文件,System.currentTimeMillis的());
        notification.flags = notification.flags | Notification.FLAG_ONGOING_EVENT;
        notification.contentView =新RemoteViews(getApplicationContext()getPackageName(),R.layout.upload_progress_bar。);
        notification.contentIntent = contentIntent;
        notification.contentView.setProgressBar(R.id.progressBar1,100,进步,FALSE);
        notificationManager.notify(42通知);
        notificationManager.notify(42通知);
        线程下载=新主题()
        {
            @覆盖
            公共无效的run()
            {
                Log.e(下载,开始);
                尝试
                {
                    的for(int i = 1; I< 100;我++)
                    {
                        进度++;
                        notification.contentView.setProgressBar(R.id.progressBar1,100,进步,FALSE);
                        如果(ⅰ== 1)
                        {
                            如果(NotificationProgressTestActivity.i == 1)
                            {
                                URL =新的URL(HTTP://xxxxxxxxxxxxxxxx.mp4);
                            }
                            否则如果(NotificationProgressTestActivity.i == 2)
                            {
                                URL =新的URL(HTTP://xxxxxxxxxxxxxxxx.mp4);
                            }
                            HttpURLConnection的C =(HttpURLConnection类)url.openConnection();
                            c.setRequestMethod(GET);
                            c.setDoOutput(真);
                            c.connect();                            字符串PATH = Environment.getExternalStorageDirectory()+/;
                            Log.e(PATH,PATH);
                            档案文件=新的文件(路径);
                            如果(!file.exists())
                            {
                                file.mkdir();
                                Log.e(目的地,创建);
                            }
                            其他
                            {
                                Log.e(目的地,存在);
                            }
                            文件OUTPUTFILE =新的文件(文件,文件名);
                            FOS的FileOutputStream =新的FileOutputStream(OUTPUTFILE);                            InputStream为= c.getInputStream();                            字节[]缓冲区=新的字节[10171188]
                            INT LEN1 = 0;
                            而((LEN1 = is.​​read(缓冲液))!= - 1)
                            {
                                fos.write(缓冲液,0,LEN1);
                            }
                            fos.close();
                            is.close();
                            // -----------------------
                            如果(!outputFile.exists())
                            {
                                Log.e(outputFile.toString(),而不是创造);
                            }
                            其他
                            {
                                Log.e(outputFile.toString(),创造);
                                Log.e(outputFile.toString(),+ outputFile.length());
                            }
                            Log.e(下载,结束);
                        }
                        notificationManager.notify(42通知);
                        尝试
                        {
                            视频下载(1017);
                        }
                        赶上(InterruptedException的E)
                        {
                            e.printStackTrace();
                        }
                 }
            }
            赶上(IOException异常E)
            {
                        Log.e(log_tag,错误:+ E);
            }
            Log.e(log_tag,检查);                //删除通知(我们就大功告成了)
            notificationManager.cancel(42);
        }
     };
        download.run();
    }


解决方案

 的PendingIntent contentIntent = PendingIntent.getActivity(这一点,0,意向,0);

在这上面的行1,在getActivity()的第二个参数是请求code,它应该是为每个按钮的唯一编号。目前它是0,这两个按钮。

  notificationManager.notify(42通知);

在上面的行2-,你是路过42通知的索引,它应该是两个按钮是唯一的。目前不管你创建了多少按钮,因为你逝去的42对每个它永远不会告诉你一个新的通知。它会保持更新当前之一。

你也可能需要再次看code你onHandleIntent正在做的()。有更好的方法来做到这一点。

in my app i have 4 buttons and when the user clicks any of the button it starts downloading a file and the progress bar gets shown in the notification area. The downloading and progress bar is working fine, but i have the following two problems

  1. When the download completes the progress bar is not getting closed, it remains in the notification area
  2. As i said above i have 4 buttons and when the first button is clicked download gets started and when the other three buttons are clicked immediately download is not taking place. I thought it may start after first download completes. But nothing happens. How to show all the progress bar when all buttons clicked

Following is my code(here i have added only 2 buttons) pls help me

b1 = (Button)findViewById(R.id.button1);
        b1.setOnClickListener(new View.OnClickListener() 
        {   
            @Override
            public void onClick(View v) 
            {
                i =1;
                Intent intent = new Intent(NotificationProgressTestActivity.this, UploadService.class);
                startService(intent);
            }
        });

        b2 = (Button)findViewById(R.id.button2);
        b2.setOnClickListener(new View.OnClickListener() 
        {   
            @Override
            public void onClick(View v) 
            {
                i = 2;
                Intent intent = new Intent(NotificationProgressTestActivity.this, UploadService.class);
                startService(intent);
            }
        });

Next following is my Uplaod Service.class

public class UploadService extends IntentService
{   
    private NotificationManager notificationManager;
    private Notification notification;
    private int progress = 10;
    private static String fileName = "folder/";
    private static URL url;
    public UploadService(String name) 
    {
        super(name);
    }
    public UploadService()
    {
        super("UploadService");
    }
    @Override
    protected void onHandleIntent(Intent intent) 
    {
        notificationManager = (NotificationManager) getApplicationContext().getSystemService(getApplicationContext().NOTIFICATION_SERVICE);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);
        notification = new Notification(R.drawable.icon,"Uploading file", System.currentTimeMillis());
        notification.flags = notification.flags | Notification.FLAG_ONGOING_EVENT;
        notification.contentView = new RemoteViews(getApplicationContext().getPackageName(), R.layout.upload_progress_bar);
        notification.contentIntent = contentIntent;
        notification.contentView.setProgressBar(R.id.progressBar1, 100, progress, false);
        notificationManager.notify(42, notification);
        notificationManager.notify(42, notification);
        Thread download = new Thread() 
        {
            @Override
            public void run() 
            {
                Log.e("download", "start");
                try
                {
                    for (int i = 1; i < 100; i++) 
                    {    
                        progress++;
                        notification.contentView.setProgressBar(R.id.progressBar1, 100, progress, false);
                        if(i==1)
                        {  
                            if(NotificationProgressTestActivity.i ==1 )
                            {
                                url = new URL("http://xxxxxxxxxxxxxxxx.mp4");
                            }
                            else if(NotificationProgressTestActivity.i == 2)
                            {
                                url = new URL("http://xxxxxxxxxxxxxxxx.mp4");
                            }
                            HttpURLConnection c = (HttpURLConnection) url.openConnection();
                            c.setRequestMethod("GET");
                            c.setDoOutput(true);
                            c.connect();

                            String PATH = Environment.getExternalStorageDirectory()+ "/";
                            Log.e("PATH:", PATH);
                            File file = new File(PATH);
                            if (!file.exists()) 
                            {
                                file.mkdir();
                                Log.e("destination", "created");
                            } 
                            else 
                            {
                                Log.e("destination", "exist");
                            }
                            File outputFile = new File(file, fileName);
                            FileOutputStream fos = new FileOutputStream(outputFile);

                            InputStream is = c.getInputStream();

                            byte[] buffer = new byte[10171188];
                            int len1 = 0;
                            while ((len1 = is.read(buffer)) != -1) 
                            {
                                fos.write(buffer, 0, len1);
                            }
                            fos.close();
                            is.close();
                            //  -----------------------
                            if (!outputFile.exists()) 
                            {
                                Log.e(outputFile.toString(), "not created");
                            }
                            else 
                            {
                                Log.e(outputFile.toString(), "created");
                                Log.e(outputFile.toString(), "" + outputFile.length());
                            }
                            Log.e("download", "end");
                        }
                        notificationManager.notify(42, notification);
                        try 
                        {
                            Thread.sleep(1017);
                        }
                        catch (InterruptedException e) 
                        {
                            e.printStackTrace();
                        }
                 }
            }
            catch (IOException e) 
            {
                        Log.e("log_tag", "Error: " + e);
            }
            Log.e("log_tag", "Check: ");

                // remove the notification (we're done)
            notificationManager.cancel(42);
        }
     };
        download.run();
    }

解决方案

PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);

1- in this above line, the 2nd parameter in getActivity() is requestCode, it should be a unique number for each button. currently it is 0 for both buttons.

notificationManager.notify(42, notification);

2- in the above line, the notification index you are passing as 42, it should be unique for both buttons. currently no matter how many buttons you create it will never show you a new notification because you are passing 42 for each. it will keep updating the current one.

also you might need to look again the code you are doing in onHandleIntent(). there are better ways to do this.

这篇关于在通知区域进度条不打烊,不启动第二进度条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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