Android设备在监测到网络连通后检查服务器是否有新文件;如果有则从服务器下载,下载过的文件不再做二次下载。 能问下具体怎么去实现吗?
你按照文件的顺序用二进制数字表示可以么,就是在移动端根据文件是第几个文件来赋值第几位是1,生成的数字post给服务器,然后服务器按位与发现为0的位就把文件发回来
第一步:在Mainfest中注册权限和网络接收器:
<!-- 操作sd卡权限 -->
<receiver android:name="com.dsw.testapplication.NetWorkReceiver">
<intent-filter >
<action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
</intent-filter>
</receiver>
然后在onReceiver中进行下载。
参照:http://blog.csdn.net/sujudz/article/details/9034023
package com.example.naruto;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
private int total = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText et_fileurl = (EditText) findViewById(R.id.fileurl);
final Button btn_down = (Button) findViewById(R.id.btn_down);
btn_down.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
new Thread(new Runnable(){
@Override
public void run() {
try {
URL url =new URL(et_fileurl.getText().toString()); //资源
ArrayList<String> strArray = new ArrayList<String> ();
String[] str = new String[3];
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);
conn.setRequestProperty("User-Agent","Mozilla/4.0 (compayible; MSIE8.0; Windows NT 5.1; Trient/4.0; .NET CLR 2.0.50727)");//伪装成浏览器
int length = conn.getContentLength();//huo qu zheng ge wen jian de chang du
if(length <0 ){ //pan duan wen jian cun bu cun zai
Toast.makeText(MainActivity.this, "文件不存在",Toast.LENGTH_SHORT).show();
return;
}
File file = new File(Environment.getExternalStorageDirectory(),getFileName(et_fileurl.getText().toString()));
RandomAccessFile randomFile = new RandomAccessFile(file, "rw");
randomFile.setLength(length);
int blocksize = length/3;
for(int i=0;i<3;i++){
int begin = i*blocksize;
int end = (i+1)*blocksize;
if(i==2){
end=length;
}
//chuang jian xin de xian cheng, xia zai wen jian
Thread t =new Thread(new DownloadRunnable(i, begin, end, file, url));
t.start();
}
}catch (MalformedURLException e){
Toast.makeText(MainActivity.this, "URL不正确",Toast.LENGTH_SHORT).show();
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private String getFileName(String url){
int index = url.lastIndexOf("/");
return url.substring(index);
}
class DownloadRunnable implements Runnable{
private int begin;
private int end;
private File file;
private URL url;
private int id;
public DownloadRunnable(int id, int begin, int end, File file, URL url){
this.file=file;
this.id=id;
this.begin=begin;
this.end=end;
this.url=url;
}
@Override
public void run() {
// xia zai xing wei de bian xie
try {
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("User-Agent","Mozilla/4.0 (compayible; MSIE8.0; Windows NT 5.1; Trient/4.0; .NET CLR 2.0.50727)");
conn.setRequestProperty("Range", "bytes=" + begin + "-" + end);
InputStream is = conn.getInputStream();
byte[] buf = new byte[1024 * 1024];
RandomAccessFile randomFile = new RandomAccessFile(file, "rw");
randomFile.seek(begin);
int len = 0;
while ((len = is.read(buf)) != -1 ){
randomFile.write(buf,0,len);
updateProgress(len); // dui jin du geng xin
Log.d("Download length: ","" + total);
}
is.close();
randomFile.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
synchronized private void updateProgress(int add){
total += add;
}
}
http://www.cnblogs.com/vir56k/archive/2011/06/28/2092426.html