从IntentService更新进度? [英] Updating progressbar from IntentService?

查看:170
本文介绍了从IntentService更新进度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图更新集成在我的通知栏的进度,但我似乎无法得到它的工作。我种得,为什么它不工作的想法,但我不知道该怎样去解决它的主意。这是code:

I'm trying to update the progressbar which is integrated in my notification bar but I can't seem to get it working. I kind of have the idea why it is not working but I have no idea of how to solve it. this is the code:

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,通知); while循环中,我做过尝试,但它冻结的应用程序和崩溃了。是否有任何其他的方式来通知进度更新通知经理。

I know I have to call : notificationManager.notify(1,notification); within the while loop and I've tried but it freezes the app and crashes it. Is there any other way to notify the notification manager of the progressbar updates.

谢谢!

推荐答案

尝试使用 notificationManager.notify(1,通知); 只有几次

int lastProgressUpdate=0;
while(...)
{
    if(progress%5==0 && progress!=lastProgressUpdate)
    {

         notificationManager.notify(1,notification);
         lastProgressUpdate=progress;
    }
}

这篇关于从IntentService更新进度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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