【问题标题】:Updating progressbar from IntentService?从 IntentService 更新进度条?
【发布时间】:2011-10-10 11:56:23
【问题描述】:

我正在尝试更新集成在通知栏中的进度条,但我似乎无法使其正常工作。我有点知道为什么它不起作用,但我不知道如何解决它。这是代码:

public class DownloadService extends IntentService{

     public DownloadService() {
        super("DownloadService");


    }

       @Override
       public void onCreate() {
           super.onCreate();
           ctx = getApplicationContext();
           root = new File(Environment.getExternalStorageDirectory()+"/folder-videos/");
           if(root.exists() && root.isDirectory()) {

           }else{
               root.mkdir();
           }          
           notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
           PendingIntent contentIntent = PendingIntent.getActivity(this, 0, null, 0);

           notification = new Notification(R.drawable.icon, "App", System.currentTimeMillis());
           contentView = new RemoteViews(getPackageName(), R.layout.progress_layout);
           notification.flags = Notification.FLAG_AUTO_CANCEL;
           notification.contentView = contentView;
           notification.contentIntent = contentIntent;
           contentView.setProgressBar(R.id.status_progress, 100, 0, false);        
           contentView.setTextViewText(R.id.status_text,"Downloading...");  

       }

        @Override
    protected void onHandleIntent(Intent intent) {
        Intent broadcastIntent = new Intent();

        int count;
        String full_url = URL + intent.getStringExtra(VIDEOS);

        try {
            URL url = new URL(full_url);
            URLConnection conexion = url.openConnection();
            conexion.connect();
            File file = new File(root.getPath(), intent.getStringExtra(VIDEOS));

            int lenghtOfFile = conexion.getContentLength();
            Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile);

            InputStream input = new BufferedInputStream(url.openStream());
            OutputStream output = new FileOutputStream(file);

            byte data[] = new byte[1024];

            long total = 0;
            contentView.setTextViewText(R.id.status_text,"Downloading " + intent.getStringExtra(VIDEOS));  
            while ((count = input.read(data)) > 0) {
               total += count;      
               notification.contentView.setProgressBar(R.id.status_progress, 100,(int)((total*100)/lenghtOfFile), false);       
               Log.e("totaltotal","" + (int)((total*100)/lenghtOfFile));
               output.write(data, 0, count);


            } 
            notificationManager.notify(1,notification);
            output.flush();
            output.close();
            input.close();
        } catch (Exception e) {
            Log.e("PRINTSTACK","STACK:" + e.getMessage());
            e.printStackTrace();
        }

    }
}

我知道我必须调用:notificationManager.notify(1,notification);在while循环中,我已经尝试过,但它冻结了应用程序并使其崩溃。有没有其他方法可以通知通知管理器进度条更新。

谢谢!!

【问题讨论】:

    标签: android


    【解决方案1】:

    尝试只使用几次notificationManager.notify(1,notification);

    如:

    int lastProgressUpdate=0;
    while(...)
    {
        if(progress%5==0 && progress!=lastProgressUpdate)
        {
    
             notificationManager.notify(1,notification);
             lastProgressUpdate=progress;
        }
    }
    

    【讨论】:

    • 用 5 尝试过它仍然会冻结 UI。尝试了 10 次,尽管程度较轻,但我仍然可以将通知栏拉下,但它会缓慢而冻结。
    • 是否有某种 NotificationListener 来监听更新并通知 NotificationManager。
    • 它像以前一样更新它,但是当我拉下通知栏时它一直冻结...
    • 在下载文件时,除了使用while循环更新进度条,还有其他方法吗!!
    • 这段代码(while 循环)在 IntentService 内部和 onHandleIntent 内部,它作为与主线程分开的线程运行,应该没问题。
    猜你喜欢
    • 2013-03-08
    • 2017-08-23
    • 2020-11-05
    • 1970-01-01
    • 1970-01-01
    • 2014-04-09
    • 2015-07-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多