第一天作业

mac2024-10-27  12

package com.example.app3; import android.os.Handler; import android.os.Message; import android.util.Log; 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; public class MyThread extends Thread { public static final int PROGRESS=0; private static final String TAG = "MyThread"; private Handler handler;//更新ui private String str_url;//网址 private String path;//路径 public MyThread(Handler handler, String str_url, String path) { this.handler = handler; this.str_url = str_url; this.path = path; } private long end;//终点 private long start;//起点 @Override public void run() { super.run(); //第一次请求 try { URL url=new URL(str_url); HttpURLConnection connection= (HttpURLConnection) url.openConnection(); connection.setReadTimeout(20000); connection.setReadTimeout(20000); connection.connect(); Log.i(TAG, "responseCode: "+connection.getResponseCode()); if (connection.getResponseCode()==200){ end=connection.getContentLength();//获得总文件长度 Log.i(TAG, "end: "+end); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } //第二次请求 File file=new File(path); if (file!=null){ start=file.length(); }else { start=0; } Log.i(TAG, "start: "+start); try { URL url=new URL(str_url); HttpURLConnection connection= (HttpURLConnection) url.openConnection(); connection.setReadTimeout(20000); connection.setReadTimeout(20000); //设置请求头 connection.setRequestProperty("Range","bytes="+start+"-"+end); connection.connect(); Log.i(TAG, "responseCode: "+connection.getResponseCode()); if (connection.getResponseCode()==206){ //边读边向sd卡写 RandomAccessFile rw = new RandomAccessFile(file, "rw");//rw 代表读和写 rw.seek(start);//seek 标记 从哪里开始读 InputStream inputStream = connection.getInputStream(); int len=0; byte[]bytes=new byte[1024]; long count=start;//已经下载了多少 while ((len=inputStream.read(bytes))!=-1){ rw.write(bytes,0,len); count+=len; //计算百分比 int progress =(int) (count * 100 / end); Message obtain = Message.obtain(); obtain.what=PROGRESS; obtain.obj=progress; handler.sendMessage(obtain); Log.i(TAG, "progress: "+progress); } } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } package com.example.app3; import android.Manifest; import android.content.pm.PackageManager; import android.os.Build; import android.os.Bundle; import android.os.Environment; import android.os.Handler; import android.os.Message; import android.support.annotation.NonNull; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.ProgressBar; import android.widget.Toast; public class MainActivity extends AppCompatActivity { private Button mainButton; private ProgressBar mainProgress; private String url="http://uvideo.spriteapp.cn/video/2019/0512/56488d0a-7465-11e9-b91b-1866daeb0df1_wpd.mp4"; private String path="/storage/emulated/0/mogu.mp4"; private Handler handler=new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); if (msg.what==MyThread.PROGRESS){ mainProgress.setProgress((Integer) msg.obj); } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}, 100); } initView(); } @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) { Toast.makeText(this, "已获得权限", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(this, "未获得权限", Toast.LENGTH_SHORT).show(); } } private void initView() { mainButton = (Button) findViewById(R.id.main_button); mainProgress = (ProgressBar) findViewById(R.id.main_progress); mainButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //下载视频 new MyThread(handler,url,path).start(); } }); } }

饿汉式单例

private Person(){ } private static Person person=new Person(); public static Person getInstance(){ return person; }

懒汉式

private Person(){ } private static Person person=null; public static Person getInstance(){ if (person==null) { synchronized (Object.class) { if (person==null){ person = new Person(); } } } return person; }
最新回复(0)