gpt4 book ai didi

java - 使用下载管理器下载时显示进度条

转载 作者:搜寻专家 更新时间:2023-11-01 08:54:26 25 4
gpt4 key购买 nike

我正在使用下载管理器 api 下载大文件,我也实现了。但问题是,下载进度通知显示在状态栏中。但我想使用进度条在 Activity 中显示通知。那么有没有人做过,如果有请帮助我。

public class DownloadManagerActivity extends Activity {
private long enqueue;
private DownloadManager dm;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
long downloadId = intent.getLongExtra(
DownloadManager.EXTRA_DOWNLOAD_ID, 0);
Query query = new Query();
query.setFilterById(enqueue);
Cursor c = dm.query(query);
if (c.moveToFirst()) {
int columnIndex = c
.getColumnIndex(DownloadManager.COLUMN_STATUS);
if (DownloadManager.STATUS_SUCCESSFUL == c
.getInt(columnIndex)) {

ImageView view = (ImageView) findViewById(R.id.imageView1);
String uriString = c
.getString(c
.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
view.setImageURI(Uri.parse(uriString));
}
}
}
}
};

registerReceiver(receiver, new IntentFilter(
DownloadManager.ACTION_DOWNLOAD_COMPLETE));
}

public void onClick(View view) {
dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
Request request = new Request(
Uri.parse("http://www.vogella.de/img/lars/LarsVogelArticle7.png"));
enqueue = dm.enqueue(request);

}

最佳答案

这是我用来在 Activity 中显示进度条并且下载管理器请求下载的代码。

final ProgressDialog progressBarDialog= new ProgressDialog(this);
progressBarDialog.setTitle("Download App Data, Please Wait");

progressBarDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressBarDialog.setButton(DialogInterface.BUTTON_POSITIVE, "OK",new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,
int whichButton){
// Toast.makeText(getBaseContext(),
// "OK clicked!", Toast.LENGTH_SHORT).show();
}
});
progressBarDialog.setProgress(0);

new Thread(new Runnable() {

@Override
public void run() {

boolean downloading = true;

DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
while (downloading) {

DownloadManager.Query q = new DownloadManager.Query();
q.setFilterById(DownloadManagerId); //filter by id which you have receieved when reqesting download from download manager
Cursor cursor = manager.query(q);
cursor.moveToFirst();
int bytes_downloaded = cursor.getInt(cursor
.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
int bytes_total = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));

if (cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS)) == DownloadManager.STATUS_SUCCESSFUL) {
downloading = false;
}

final int dl_progress = (int) ((bytes_downloaded * 100l) / bytes_total);

runOnUiThread(new Runnable() {

@Override
public void run() {

progressBarDialog.setProgress((int) dl_progress);

}
});

// Log.d(Constants.MAIN_VIEW_ACTIVITY, statusMessage(cursor));
cursor.close();
}

}
}).start();


//show the dialog
progressBarDialog.show();

}

}

关于java - 使用下载管理器下载时显示进度条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20092460/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com