在Android中使用AsyncTask下载多个文件






4.22/5 (8投票s)
本文档将教你如何使用进度条、通知和 AsyncTask 下载多个文件。
引言
本文档展示了如何使用进度条、通知和 AsyncTask
下载多个文件。
使用代码
- 首先要做的是使用
isInternetConnectionActive(context)
方法检查连接。如果此方法返回 true,则创建一个通知来显示消息。 - 在
showNotification()
方法中,我们定义了一个Intent
,将其第二个参数声明为NotifyPage.class
。当用户选择通知时,调用活动NotifyPage
。对于此活动,设计一个包含两个按钮的 XML 文件:一个用于创建另一个通知以显示progressBar()
的按钮,以及一个用于取消下载操作并返回主页的按钮。 - 在
DownloadTask
类中,你可以发送多个文件,但在本例中,我们只发送一个文件地址。实际上,使用execute(url)
,你调用doInBackground()
,它在后台运行下载操作。你可以在DownloadTask
类中使用四种方法调用下载通知。
private static final int HELLO_ID = 1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
if(isInternetConnectionActive(getApplicationContext())) {
showNotification(1);
}
else {
showNotification(0);
}
}
private boolean isInternetConnectionActive(Context context) {
NetworkInfo networkInfo = ((ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE))
.getActiveNetworkInfo();
if(networkInfo == null || !networkInfo.isConnected()) {
return false;
}
return true;
}
private void showNotification(int status){
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(ns);
int icon = android.R.drawable.ic_media_play;
CharSequence ticketText = "Connection";
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, ticketText, when);
Context context = getApplicationContext();
CharSequence contentTitle = "Internet Connection";
CharSequence contentText = "";
if(status == 1){
contentText = "Internet connected, Are you Update your Application?";
}
else{
contentText = "Internet disconnected!";
}
Intent notificationIntent = new Intent(this, NotifyPage.class);
notificationIntent.addFlags(Notification.FLAG_ONGOING_EVENT);
PendingIntent contentIntent =
PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
mNotificationManager.notify(HELLO_ID, notification);
}
public void clearNotification(View v) {
mNotificationManager.cancel(1);
finish();
}
public void UpdateApplication(View v){
mNotificationManager.cancel(1);
new DownloadTask(this).execute("http://animal.discovery.com/" +
"birds/peacock/pictures/peacock-picture.jpg");
}
public DownloadTask(Context context){
this.context = context;
//Create the notification object from NotificationHelper class
mNotificationHelper = new NotificationHelper(context);
}
protected void onPreExecute(){
//Create the notification in the statusbar
mNotificationHelper.createNotification();
}
protected void onProgressUpdate(Integer... progress) {
//This method runs on the UI thread, it receives progress updates
//from the background thread and publishes them to the status bar
mNotificationHelper.progressUpdate(progress[0]);
}
protected void onPostExecute(Void result) {
//The task is complete, tell the status bar about it
mNotificationHelper.completed();
}
请记住在 AndroidManifest.xml 中定义与连接相关的权限:android.permission.INTERNET
- android.permission.ACCESS_NETWORK_STATE
。
关注点
希望本文档能教你如何创建状态栏通知并使用 AsyncTask
类来下载或上传文件。