异步
public class MyAsyncTask1 extends AsyncTask<String,Integer,String> {
private Context context;
private List<JavaBean.DataBean> list;
private ProgressDialog progressDialog;
private String path;
private long maxLen;
private long start;
public MyAsyncTask1(Context context, List<JavaBean.DataBean> list, String path) {
this.context = context;
this.list = list;
this.path = path;
progressDialog=new ProgressDialog(context);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setTitle("正在下载");
progressDialog.setMessage("拼命为您下载中!!");
}
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog.show();
}
@Override
protected String doInBackground(String... strings) {
try {
URL url = new URL(strings[0]);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.connect();
if(connection.getResponseCode()==200) {
maxLen = connection.getContentLength();
}
} catch (IOException e) {
e.printStackTrace();
}
File file1 = new File(path);
if (file1.exists()){
start=file1.length();
}else {
start=0;
}
try {
URL url = new URL(strings[0]);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("Range","bytes="+start+"-"+maxLen);;
connection.connect();
if(connection.getResponseCode()==206) {
InputStream is = connection.getInputStream();
File file3 = new File(path);
RandomAccessFile rw = new RandomAccessFile(file3, "rw");
rw.seek(start);
int len=0;
byte[] b = new byte[1024];
long curLen=start;
while ((len=is.read(b))!=-1){
rw.write(b,0,len);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
curLen+=len;
int progress= (int) ((curLen*100)/maxLen);
publishProgress(progress);
}
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
progressDialog.setProgress(values[0]);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
if (progressDialog.getProgress()==100){
progressDialog.dismiss();
Toast.makeText(context, "下载完毕", Toast.LENGTH_SHORT).show();
}else {
File file1 = new File(path);
if (file1.exists()){
progressDialog.dismiss();
Toast.makeText(context, "已存在", Toast.LENGTH_SHORT).show();
}
}
}
}
主类
public class MainActivity extends AppCompatActivity {
private ListView lv1;
private MyAdapter myAdapter;
private List<JavaBean.DataBean> list = new ArrayList<>();
private Button dian;
private static final String TAG = "MainActivity";
private String path="/storage/emulated/0/Pictures/";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE},100);
}
private void initView() {
lv1 = (ListView) findViewById(R.id.lv_1);
dian = (Button) findViewById(R.id.dian);
new MyAsyncTask(MainActivity.this, list).execute("http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=20&page=1");
dian.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dian.setVisibility(View.GONE);
myAdapter = new MyAdapter(MainActivity.this, list);
Log.d(TAG, "onClick: "+list);
lv1.setAdapter(myAdapter);
}
});
lv1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
builder.setTitle("下载吗?老弟");
builder.setMessage("是否下载图片?");
builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
new MyAsyncTask1(MainActivity.this,list,path+"第"+position+"图.jpg").execute(list.get(position).getPic());
}
}).setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
}).show();
}
});
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode==100 && grantResults[0]== PackageManager.PERMISSION_GRANTED){
initView();
}else {
finish();
}
}
}