利用近期學(xué)習(xí)的知識(shí)做一個(gè)下載器桂敛,輸入網(wǎng)址即可下載的下載器功炮。
首先復(fù)習(xí)上節(jié)課的知識(shí)。
多線程的那些事:
->何為線程术唬,多線程薪伏?
->為什么要使用多線程?
幫助提高效率粗仓,耗時(shí)操作并行嫁怀。多線程可防止一條線卡死。
->ANR(Application Not Responding)
->什么時(shí)候使用多線程借浊?
->Android當(dāng)中的多線程眶掌。
總結(jié)來(lái)說(shuō):線程就相當(dāng)于做飯的過(guò)程,OnCreate一步一步按順序執(zhí)行:摘菜->洗菜->切菜->做菜->吃菜巴碗。 電飯鍋相當(dāng)于另外一個(gè)線程朴爬,幫做米飯,同時(shí)你吃菜橡淆,電飯鍋會(huì)在蒸熟米飯后語(yǔ)音或其他方式提醒召噩。
如何使用多線程母赵?
->Main/UI Thread和Worker Thread。
->Main/UI Thread和Worker Thread之間的通信具滴。
->Thread/Runnable.
->線程安全
->Activity.runOnUiThread(Runnable)
->View.post(Runnable)
->View.post Delayed(Runnable,long)
->Handler
->Async Task
->同步代碼塊(Synchronized block).
下載器的制作方法:
在activity_main.xml文件中做好下載器的下載界面:
下載按鈕:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/download"
android:id="@+id/download_button"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
輸入網(wǎng)址的文本框:
<EditText
android:layout_width="400dp"
android:layout_height="50dp"
android:id="@+id/EditText"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="84dp" />
進(jìn)度條:
<ProgressBar
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/progressBar"
android:max="100"
android:layout_below="@+id/download_button"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="72dp" />
進(jìn)度更新顯示:
<TextView
android:id="@+id/text_view"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text=" "
android:textColor="@color/blue"
android:layout_below="@+id/progressBar"
android:layout_alignRight="@+id/download_button"
android:layout_alignEnd="@+id/download_button" />
輸入網(wǎng)址提示:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="請(qǐng)輸入網(wǎng)址:"
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
在MainActivity.java文件中:
public class MainActivity extends Activity implements View.OnClickListener{
public static final String GEEK_BAND = "RENHONG";
private Handler mHandler = new DownloadHandler(this);
private TextView mTextView;
private Button mDownloadButton;
private ProgressBar mProgressBar;
private EditText mEditText;
public TextView getmTextView() {
return mTextView;
}
public ProgressBar getmProgressBar() {
return mProgressBar;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextView = (TextView) findViewById(R.id.text_view);
mEditText=(EditText) findViewById(R.id.EditText);
mDownloadButton = (Button) findViewById(R.id.download_button);
mProgressBar = (ProgressBar) findViewById(R.id.progressBar);
mDownloadButton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.download_button:
String apkurl=mEditText.getText().toString();
new TestTask().execute(apkurl);
break;
}
}
class TestTask extends AsyncTask<String,Integer,String>{
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String...prams) {
try {
URL url = new URL(prams[0]);
URLConnection urlConnection = url.openConnection();
InputStream inputStream = urlConnection.getInputStream();
int contentLength = urlConnection.getContentLength();
// 要下載的文件的大小
String downloadFoldersName = Environment.getExternalStorageDirectory() + File.separator + GEEK_BAND + File.separator;
File file = new File(downloadFoldersName);
if(!file.exists()){
file.mkdir();
}
String fileName = downloadFoldersName + "test.apk";
File apkFile = new File(fileName);
if(apkFile.exists()){
apkFile.delete();
}
int downloadSize = 0;
byte[] bytes = new byte[1024];
int length = 0;
OutputStream outputStream = new FileOutputStream(fileName);
while ((length = inputStream.read(bytes)) != -1){
outputStream.write(bytes, 0, length);
downloadSize += length;
int progress = downloadSize * 100/ contentLength;
Message message = mHandler.obtainMessage();
message.obj = progress;
message.what = 0;
mHandler.sendMessage(message);
}
inputStream.close();
outputStream.close();
}
catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
}
}
public static class DownloadHandler extends Handler{
public final WeakReference<MainActivity>mActivity;
public DownloadHandler(MainActivity activity){
mActivity = new WeakReference<>(activity);
}
public void handleMessage(Message msg){
super.handleMessage(msg);
MainActivity activity = mActivity.get();
switch (msg.what){
case 0:
int progress = (int)msg.obj;
activity.getmProgressBar().setProgress(progress);
activity.getmTextView().setText("已下載:"+progress);
if (progress == 100){
Toast.makeText(activity,"下載完成",Toast.LENGTH_SHORT).show();
}
break;
}
}
}
}
在Mainifest.xml文件中注冊(cè)網(wǎng)絡(luò):
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<uses-permission android:name="android.permission.INTERNET" />