我想从服务器下载数据。所以当前活动在屏幕上显示下载进度条。如果我移动到另一个活动并返回下载页面活动页面,我想显示下载光标以及下载速率和大小interupt.我应该在android中使用什么类型的服务?谁能给我一些android中的逻辑或代码?
问问题
206 次
1 回答
0
使用服务开始下载过程。要下载数据,请使用 AsyncTask。
像这样的东西:
//in activity
Intent service = new Intent (this, Service.class);
service.setAction(ACTION);//ACTION is some String constant to determine action
startService(service);
// in service
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent.getAction().equals(ACTION )){
YourAsyncTask task = new YourAsyncTask();
task .execute();
}
return Service.START_NOT_STICKY;}
因此,您开始下载,即使您关闭活动,过程也不会终止。要检查下载过程,您可以使用本地广播接收器。
于 2013-07-12T17:18:47.767 回答