warace 2011-08-16 20:43
浏览 210
已采纳

hi 问几个android开发的问题

hi 问几个问题
1. 如果在ProgressBar上显示数字,是否原生支持
2.
我有个下载的业务 在一个Activity中点下载的同时可以在另外一个Activity中显示现在进度:
我直接诶在Activity中new Thread来作的,这样好不好的,一般时用什么方法作? 这样性能是否过得去
看我红色标题部分,我是通过Thread while true,

  1. 我在已下载完毕的文件图标上放个勾勾,这个是图片合成作的,还是用android提供的工具做的?

[code="java"]
/**

  • */ package com.alibaba.cloudspace;

import java.util.ArrayList;
import java.util.List;

import android.app.TabActivity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.GestureDetector;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.widget.ListView;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.TabHost.OnTabChangeListener;

public class TransStatusActivity extends TabActivity implements OnTabChangeListener {

private GestureDetector             gestureDetector;
private UpdateProgressBarHandler    progressBarHandler;
private UpdateDownloadFilesHandler  downloadFilesHandler;
private ProgressBarListAdapter      adapter                 = null;
private DownloadCompleteListAdapter downloadCompleteAdapter = null;
private ListView                    listView                = null;
private ListView                    listView2               = null;
private TabHost                     transTabhost;
private final String[]              TAB_IDS                 = new String[] { "TRANS_TAB1",
        "TRANS_TAB2"                                       };
private static FileMap              DOWNLOAD_FILES          = new FileMap();
private String                      email                   = "";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // TODO Get email.
    // email = getIntent().getExtras().getString("");

    gestureDetector = new GestureDetector(this, new CLGestureDetector(this));

    transTabhost = this.getTabHost();
    LayoutInflater.from(this).inflate(R.layout.trans_status, transTabhost.getTabContentView(),
            true);

    transTabhost.addTab(transTabhost.newTabSpec(TAB_IDS[0]).setIndicator(
            createTabTextView(R.string.app_text_transfer)).setContent(R.id.transfer_tab));

    transTabhost.addTab(transTabhost.newTabSpec(TAB_IDS[1]).setIndicator(
            createTabTextView(R.string.app_text_transfer_finish)).setContent(
            R.id.transfer_finish_tab));

    // TODO 载入未完成的下载列表
    progressBarHandler = new UpdateProgressBarHandler();
    downloadFilesHandler = new UpdateDownloadFilesHandler();

    adapter = new ProgressBarListAdapter(this, getTransStatusList());

    listView = (ListView) findViewById(R.id.trans_status_list);
    listView.setAdapter(adapter);

   [color=red][b] new UpdateProgressBarThread().start();
    new UpdateDownloadFileListThread().start();[/b][/color]
    downloadCompleteAdapter = new DownloadCompleteListAdapter(this, DOWNLOAD_FILES.get(email));

    listView2 = (ListView) findViewById(R.id.download_complete_list);
    listView2.setAdapter(downloadCompleteAdapter);

    transTabhost.setOnTabChangedListener(this);
}

。。。。
private List getTransStatusList() {
List transStatus = new ArrayList();

    for (int i = 0; i < FileListActivity.downloading.size(); i++) {
        FileDownloaderTask t = FileListActivity.downloading.get(i);
        if (t.isCompleted()) {
            DOWNLOAD_FILES.add(email, t.getFile());
            downloadFilesHandler.sendMessage(new Message());

            FileListActivity.downloading.remove(i--);
            continue;
        }

        TransStatus s = new TransStatus();
        s.setFid(t.getFile().getNodeId());
        s.setTitle(t.getFile().getFileName());
        s.setPercent(t.getPercent());

        Log.d("DEBUG-TRANS-STATUS", "Percent:" + s.getPercent() + ", has read:"
                + t.getHasReadSize() + ", full size:" + t.getFile().getFullSize());
        transStatus.add(s);
    }

    return transStatus;
}

class UpdateProgressBarThread extends Thread {
    public void run() {
        while (true) {
            Message msg = new Message();
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {

            }

            progressBarHandler.sendMessage(msg);

            int i = 0;
            for (FileDownloaderTask f : FileListActivity.downloading) {
                if (f.isCompleted())
                    i++;
            }

            Log.e("", "Completed:" + i + ", downloading size:"
                    + FileListActivity.downloading.size());
            if (i >= FileListActivity.downloading.size())
                break;
        }
    };
}

class UpdateDownloadFileListThread extends Thread {

    public void run() {
        Message msg = new Message();
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {

        }

        downloadFilesHandler.sendMessage(msg);
    };
}

class UpdateProgressBarHandler extends Handler {

    @Override
    public void handleMessage(Message msg) {
        adapter = new ProgressBarListAdapter(TransStatusActivity.this, getTransStatusList());

        listView = (ListView) findViewById(R.id.trans_status_list);
        listView.setAdapter(adapter);
    }
}

class UpdateDownloadFilesHandler extends Handler {

    @Override
    public void handleMessage(Message msg) {
        downloadCompleteAdapter = new DownloadCompleteListAdapter(TransStatusActivity.this,
                DOWNLOAD_FILES.get(email));

        listView2 = (ListView) findViewById(R.id.download_complete_list);
        listView2.setAdapter(downloadCompleteAdapter);
    }
}

@Override
public void onTabChanged(String tabId) {

}

private TextView createTabTextView(int rid) {
    TextView txtTabInfo = new TextView(this);
    txtTabInfo.setText(getResources().getString(rid));
    txtTabInfo.setPadding(0, 5, 0, 0);
    txtTabInfo.setTextSize(11);

    txtTabInfo.setBackgroundResource(R.drawable.tab_normal);
    txtTabInfo.setTextColor(Color.DKGRAY);
    txtTabInfo.setGravity(Gravity.CENTER);
    txtTabInfo.setHeight(39);

    return txtTabInfo;

}

}

[/code]

  • 写回答

1条回答 默认 最新

  • rainbow702 2011-08-22 09:33
    关注
    1. Progress Bar 是不带数字显示的
    2. 最好不要直接使用 new Thread 来完成下载,建议你使用 AsyncTask 去实现
    3. 没看到你的效果图,不知道你是指什么?但是我觉得应该是这样的:文件正在下载时,你显示的是一个图标,而当文件下载完成时,用另外一个图标来代替之前的图标
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?