显示通知栏中的进度

时间:2012-05-07 13:41:44

标签: android

我使用下面的代码显示通知区域中的进度条。我不确定这是对的。请检查并让我知道。我想在视频上传时显示进度条,并希望在上传后关闭。请给我建议。

protected Void doInBackground(Void... arg0) {   

            Intent intent = new Intent();
            final PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);
            // configure the notification          
            notification.flags = notification.flags | Notification.FLAG_ONGOING_EVENT;
            notification.contentView = new RemoteViews(getApplicationContext().getPackageName(), R.layout.uploadprogress);
            notification.contentIntent = pendingIntent;
            notification.contentView.setImageViewResource(R.id.status_icon, R.drawable.progressbar);
            notification.contentView.setTextViewText(R.id.status_text, "Uploading...");
            notification.contentView.setProgressBar(R.id.status_progress, 100, progress, false);

            notificationManager = (NotificationManager) getApplicationContext().getSystemService(
                    getApplicationContext().NOTIFICATION_SERVICE);

            notificationManager.notify(10, notification);

            final SharedPreferences prefs = PreferenceManager
            .getDefaultSharedPreferences(getApplicationContext());
            UUID uniqueKey = UUID.randomUUID();
                fname = uniqueKey.toString();
                Log.e("UNIQUE NAME", fname);
                String hostName = "MY HOST NAME";
                String username = "test";
                String password = "****";
                String location = selectedPath;         
                InputStream in = null;
                try {
                    Thread upload = new Thread() {

                        @Override
                        public void run() {

                            for (int i = 1; i < 100; i++) {   
                                progress=progress+1;
                                notification.contentView.setProgressBar(R.id.status_progress, 100, progress, false);                               
                                // inform the progress bar of updates in progress
                                notificationManager.notify(id, notification);                      
                                try {
                                    Thread.sleep(2000);
                                } catch (InterruptedException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                }
                            }      
                        }
                    };
                    upload.run();
                    FTPClient ftp = new FTPClient();
                    ftp.connect(hostName);
                    ftp.login(username, password);

                    ftp.setFileType(FTP.BINARY_FILE_TYPE);
                    ftp.enterLocalPassiveMode();
                    ftp.changeWorkingDirectory("/uploads");

                    int reply = ftp.getReplyCode();
                    System.out.println("Received Reply from FTP Connection:" + reply);

                    if (FTPReply.isPositiveCompletion(reply)) {
                        System.out.println("Connected Success");
                    }

                    File f1 = new File(location);
                    in = new FileInputStream(f1);

                    ftp.storeFile(fname+"."+extension, in);

                    System.out.println("SUCCESS");
                    System.out.println("Video Title:" +strTitle+" is uploaded successfully");
                    System.out.println("Video Name:" +fname+" is uploaded successfully");
                    ftp.logout();
                    ftp.disconnect();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                StringBuffer inStreamBuf = new StringBuffer();
                strUserName = prefs.getString(
                        getResources().getString(R.string.username), null);
                strToken = prefs.getString(
                        getResources().getString(R.string.token), null);
                url = prefs.getString("VALUE", null);
                try {                   
                        inStreamBuf = XmlUtil.getUploadAuthResponse(url,
                                strUserName, strToken, strType, strTitle,
                                fname, strDesc, strCatId, strTags, strAccess,
                                strComments, strEmbed, strRating, strPublish);
                        strXmlResponse = inStreamBuf.toString();
                        Log.e("Response:", strXmlResponse);
                        DocumentBuilder db = DocumentBuilderFactory
                                .newInstance().newDocumentBuilder();
                        InputSource is = new InputSource();
                        is.setCharacterStream(new StringReader(strXmlResponse));
                        Document doc = db.parse(is);
                        NodeList nodes = doc
                                .getElementsByTagName(getResources().getString(
                                        R.string.xmlResponse));
                        Element element = (Element) nodes.item(0);
                        NodeList nl_request_type = element
                                .getElementsByTagName(getResources().getString(
                                        R.string.response));
                        Element el_request_type = (Element) nl_request_type
                                .item(0);
                        String strType = getCharacterDataFromElement(el_request_type);
                        // System.out.println("Response----------->"+strType);
                        if (strType.equalsIgnoreCase(getResources().getString(
                                R.string.success))) {   
                            finish();
                            Intent intent1 = new Intent(NewVideoActivity.this,
                                    MainscreenActivity.class);
                            startActivity(intent);
                        } else if (strType.equalsIgnoreCase(getResources()
                                .getString(R.string.expired))) {
                            XmlUtil.session = null;
                            Intent intent2 = new Intent(NewVideoActivity.this,
                                    MainscreenActivity.class);
                            startActivity(intent);
                            finish();
                            System.exit(0);
                        }                                           
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }   
                notificationManager.cancel(10); 
                return null;

            }

0 个答案:

没有答案