读写SDCard
Android高版本不能使用http协议,需要在清单文件中添加(application内)
android:usesCleartextTraffic="true"
|
在清单文件里面申请使用网络权限、读SD卡的权限、写SD卡的权限、管理SD卡的权限
<uses-permission android:name="android.permisson.INTERNET"></uses-permission> <uses-permission android:name="android.permisson.READ_EXTERNAL_STORAGE"></uses-permission> <uses-permission android:name="android.permisson.WRITE_EXTERNAL_STORAGE"></uses-permission> <uses-permission android:name="android.permisson.MANAGE_EXTERNAL_STORAGE"></uses-permission>
|
MainActivity.java,部分代码
public class MainActivity extends AppCompatActivity { TextView tv = findViewById(R.id.tv_downloadResult); tv.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { DownloadZip downloadZip = new DownloadZip(tv); downloadZip.execute("http:192.168.31.197/down/test/zip"); } }); findViedById(R.id.btn_startUnzip).setOnClickListener(new View.OnClickListener() { @Override public void onClick((View view)) { UnzipFile unzipFile = new UnzipFile(tv); unzipFile.execute(""); } }); }
|
DownloadZip.java
public class DownloadZip extends AsyncTask<String,String,String> { private TextView tv; private String ret = ""; public DownloadZip(TextView ptv) { tv = ptv; } @Override protected String doInBackground(String...String) { HttpURLConnection httpURLConnection = null; InputStream inputStream = null; FileOutputStream outputStreamToFile = null; try { URL url = new URL(string[0]); httpURLConnection = (HttpURLConnection) url.openConnection(); httpURLConnection.connect(); if(httpURLConnection.getResponseCode != HttpURLConnection.HTTP_OK) { Log.d("DownloadZip","nor error code" + httpURLConnection.getResponseCode()); return "code error"; } inputStream = httpURLConnection.getInputStream(); File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS+"/"+"zipSaveDir/"); if(!dir.exists()) { dir.mkdir } String fullFilePath = dir + "/" +"2058.zip"; File file = new File(fullFilePath); if(file.exists) { file.delete(); } file.createNewFile(); outputStreamToFile = new File(file.getAbsolutePath); byte[] buffer = new byte[1024]; int count; while(true) { count = inputStream.read(buffer); if(count == -1) { break; } outputStreamFile.write(buffer,0,count); } outputStreamToFile.close(); ret = "success"; }catch(Exception ee) { ee.printStackTrace(); ret = "fail"; }finally { try { if(inputStream !=null) { inputStream.close(); } if(httpURLConnection !=null) { httpURLConnection.disconnect(); } }catch(Exception ee) { ee.pringStackTrace; } } return ret; } @Override protected void onPostExecute(String s) { super.onPostExecute(s); if(s.equals("success")) { tv.setText("下载success"); } else { tv.setText("下载failed"); } } }
|
UnzipFile.java
public class UnzipFile extends AsyncTask<String, String, String> { private String ret = ""; public UnzipFile(TextView ptv) { tv = ptv; } @Override protected String doInBackground(String...strings) { InputStream inputStream = null; ZipInputStream zipInputStream = null; OutputStream outputStream = null; try { File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS + "/" +"zipSaveDir/"); String fullFilePath = dir + "/" +"2058.zip"; File file = new File(fullFilePath); if(file.exists()) { String jieyaDir = dir.getAbsolutePath(); inputStream = new FileInputStream(file); zipInputStream = new ZipInputStream(new BufferedInputStream(inputStream)); byte[] buffer = new byte[1024]; ZipEntry zipEntry; while((zipEntry = zipInputStream.getNextEntry())!=null) { String fileName = zipEntry.getName(); if(zipEntry.isDirectory()) { File subDir = new File(jieyaDir + "/" + fileName); subDir.mkdirs(); continue; } int rCount; FileOutputStream fout = new FileOutputStream(jieyaDir + "/"+ fileName); while((rCount = zipInputStream.read(buffer))!=-1) { fout.write(buffer,0,rCount); } fout.close(); zipInputStream.closeEntry(); } zipInputStream.close(); ret = "unzip success"; }else { ret = "file not exists"; } }catch(Exception ee) { ee.printStackTrace(); } return ret; @Overrride protected void onPostExecute(String s) { super.onPostExecute(s); } if(s.equals("unzip success")) { tv.setText("解压成功"); } else { tv.setText("解压失败"); } } }
|
Handler
MainActivity,部分代码
public class MainActivity extends AppCompatActivity { private MyHandler myHandler; TextView tv; public void updateUI01() { tv.setText("01"); } public void updateUI02() { tv.setText("02"); } public void updateUI03() { tv.setText("03"); } @Override protected void onCreate(Bundle saveInstanceState) { super.onCreate(savaInstanceState); setContentView(R.layout.activity_main); SubThreadCreateHandler subThread = new SubThreadCreateHandler(); subThread.start(); myHandler = new MyHandler(this); SubThreadCreateHandler subThread = new SubThreadCreateHandler(); subThread.start(); MyThread myThread = new MyThread(myHandler); myThread.start(); myHandler.post(new Runnable)() { @Override public void run() { for(int i=0;i<5;i++) { try { Thread.sleep(2000); } catch(InterruptedException e) { e.printStackTrace; } updateUI01(); updateUI02(); tv.setText("i:"+i); } } } Message message = Message.obtain(); message.what = 99; Bundle bundle = new Bunlde(); bundle.putString("key1","这条内容先放入Bundle,然后通过Messsage传递"); message.setData(bundle); subThred.subHandler.sendMessage(meassage); } }
|
MyHandler
public class MyHandler(MainActivity pMainActivity) { mainActivity = pMainActivity; } @Override public void handleMessage(@NoNull Message msg) { super.handleMessage(msg); Log.i("qqqqqqqq","in handleMessage"); switch(msg.what) { case 1: { mainActivity.updateUI01(); } case 2: { mainActivity.updateUI02(); } case 3: { mianActivity.updateUI03(); } } }
|
MyThread.java
public class MyThread extends Thread { private MyHandler mh; public MyThread(MyHandler mh) { this.mh = mh; } @Override public void run() { super.run(); for(int i = 0;i<5;i++) { try { Thread.sleep(200); }catch(InterruptedException e) { e.printStackTrace; } Message message = Message.obtain(); message.what = i; mh.sendMessage(message); } } }
|
SubThreadCreateHandler.java
public class SubThreadCreateHandler extends Thread { public SubHandler subHandler; public class SubHandler extends Handler { public class SubHandler (Looper myLooper){} @Override public void handleMessage(@NonNull Message msg) { super.handleMessage(msg); Log.i("qqqqqqqq","in subHandler"); Bundle data = msg.getData(); Log.i("qqqqqqqq","get String:"+data.getString("key1")); } @Override public void run() { super.run(); Looper.prepare(); subHandler = new SubHandler(Looper.myLooper()); Looper.loop(); } } }
|