回答
请find此链接。它解释了如何从互联网上下载文件。你必须把这段代码放在一个线程中。它用于后台进程。您应该参考后台进程的线程或使用AsyncTask,后者也用于后台进程。
如果您的应用程序正在使用2.3,那么您可以使用android SDK下提供的DownloadManager api。否则你可以自己编写service。
我可以请告诉我如何实现2.1和2.2下载管理器,我有服务的工作,但我不知道如何下载文件,如果有任何链接,请发送给我 – 2011-04-26 09:10:52
DownloadManager只能从2.3开始。对于较低的api版本,使用Chirag提供的链接。 – mudit 2011-04-26 11:40:39
由于Android是开源的,你可以移植下载管理器在Android 2.3的较低的Android版本
这是另一个代码使用ASYC任务与countinues通知
public class DownloadTask extends AsyncTask<Integer, Integer, Void>{
private NotificationHelper mNotificationHelper;
private static final String PEFERENCE_FILE = "preference";
private static final String ISDOWNLOADED = "isdownloaded";
SharedPreferences settings;
SharedPreferences.Editor editor;
Context context;
public DownloadTask(Context context){
this.context = context;
mNotificationHelper = new NotificationHelper(context);
}
protected void onPreExecute(){
//Create the notification in the statusbar
mNotificationHelper.createNotification();
}
@Override
protected Void doInBackground(Integer... integers) {
//This is where we would do the actual download stuff
//for now I'm just going to loop for 10 seconds
// publishing progress every second
int count;
try {
URL url = new URL("filename url");
URLConnection connexion = url.openConnection();
connexion.connect();
int lenghtOfFile = connexion.getContentLength();
Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile);
InputStream input = new BufferedInputStream(url.openStream());
//OutputStream output = new FileOutputStream("/sdcard/foldername/temp.zip");
OutputStream output = new FileOutputStream("/sdcard/foldername/himages.zip");
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
//publishProgress(""+(int)((total*100)/lenghtOfFile));
Log.d("%Percentage%",""+(int)((total*100)/lenghtOfFile));
onProgressUpdate((int)((total*100)/lenghtOfFile));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
File file = new File(Environment.getExternalStorageDirectory()
+ "/foldername/"+"_images.zip");
File path = new File(Environment.getExternalStorageDirectory()
+ "/foldername");
try {
ZipUtil.unzip(file,path);
settings = this.context.getSharedPreferences(PEFERENCE_FILE, 0);
editor = settings.edit();
editor.putBoolean(ISDOWNLOADED, true);
editor.commit();
} catch (IOException e) {
Log.d("ZIP UTILL",e.toString());
}
} catch (Exception e) {}
return null;
}
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
HyundaiApplication.serviceState=false;
mNotificationHelper.completed();
}
}
到downloaf文件这个通知助手
public class NotificationHelper {
private Context mContext;
private int NOTIFICATION_ID = 1;
private Notification mNotification;
private NotificationManager mNotificationManager;
private PendingIntent mContentIntent;
private CharSequence mContentTitle;
public NotificationHelper(Context context)
{
mContext = context;
}
/**
* Put the notification into the status bar
*/
public void createNotification() {
//get the notification manager
mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
//create the notification
int icon = android.R.drawable.stat_sys_download;
CharSequence tickerText = mContext.getString(R.string.download_ticker); //Initial text that appears in the status bar
long when = System.currentTimeMillis();
mNotification = new Notification(icon, tickerText, when);
//create the content which is shown in the notification pulldown
mContentTitle = mContext.getString(R.string.content_title); //Full title of the notification in the pull down
CharSequence contentText = "0% complete"; //Text of the notification in the pull down
//you have to set a PendingIntent on a notification to tell the system what you want it to do when the notification is selected
//I don't want to use this here so I'm just creating a blank one
Intent notificationIntent = new Intent();
mContentIntent = PendingIntent.getActivity(mContext, 0, notificationIntent, 0);
//add the additional content and intent to the notification
mNotification.setLatestEventInfo(mContext, mContentTitle, contentText, mContentIntent);
//make this notification appear in the 'Ongoing events' section
mNotification.flags = Notification.FLAG_ONGOING_EVENT;
//show the notification
mNotificationManager.notify(NOTIFICATION_ID, mNotification);
}
/**
* Receives progress updates from the background task and updates the status bar notification appropriately
* @param percentageComplete
*/
public void progressUpdate(int percentageComplete) {
//build up the new status message
CharSequence contentText = percentageComplete + "% complete";
//publish it to the status bar
mNotification.setLatestEventInfo(mContext, mContentTitle, contentText, mContentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mNotification);
}
/**
* called when the background task is complete, this removes the notification from the status bar.
* We could also use this to add a new ‘task complete’ notification
*/
public void completed() {
//remove the notification from the status bar
mNotificationManager.cancel(NOTIFICATION_ID);
}
}
谢谢。
这是非常错误的:'onProgressUpdate((int)((total * 100)/ lenghtOfFile));'。你从工作者线程调用方法而不是UI线程。改用'publishProgress()'。 – JohnD 2012-08-06 15:08:59
-1糟糕的建议,不好的形式,请参阅上面的@ JohnD的评论。 – mikebabcock 2012-12-19 20:35:08
- 1. 文件的后台下载
- 2. Angular2:从后台下载xls文件
- 3. 如何提高Android中后台下载文件的速度?
- 4. 的Android后台下载来统计
- 5. 在Android中下载后打开下载的文件
- 6. Swift - 在后台下载NSURLSession?
- 7. ASIHTTPRequest和后台下载
- 8. 下载并在后台
- 9. MVC3 Ajax.BeginForm下载后文件
- 10. 下载后移动文件
- 11. 下载后文件损坏
- 12. perl post后下载文件
- 13. 下载后文件损坏
- 14. 使用android下载文件
- 15. Android https下载文件
- 16. Android Dropbox API文件下载
- 17. Android下载html文件
- 18. 下载Android MyFirstApp文件
- 19. Android的MP3文件下载
- 20. Android的下载大文件
- 21. 安卓android文件下载
- 22. Android缓慢文件下载
- 23. 在Android上下载文件
- 24. 文件下载Android版
- 25. 在Android中下载文件
- 26. Android文件没有下载
- 27. 在后台下载和解压zip文件?
- 28. 即使应用程序进入后台,如何下载文件?
- 29. 在后台状态下载多个小文件
- 30. 下载后台线程中的多个文件?
链接已损坏。 404. – 2016-01-02 13:58:29
该链接无法正常工作。请在这里提供解决方案 – 2016-06-27 16:08:33
我已更新链接。 – 2017-06-30 10:11:12