导包
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.daimajia.numberprogressbar:library:1.4@aar'
主布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.andy.downapk.MainActivity"
tools:layout_editor_absoluteY="81dp"
tools:layout_editor_absoluteX="0dp">
<Button
android:id="@+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开始下载"
android:onClick="onClick"
app:layout_constraintRight_toLeftOf="@+id/guideline2"
android:layout_marginRight="0dp"
android:layout_marginLeft="0dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintHorizontal_bias="0.596"
app:layout_constraintTop_toTopOf="@+id/guideline"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"/>
<Button
android:id="@+id/pause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="暂停下载"
android:onClick="onClick"
android:layout_marginLeft="0dp"
app:layout_constraintLeft_toLeftOf="@+id/guideline2"
android:layout_marginRight="0dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="@+id/start"
android:layout_marginBottom="0dp"/>
<TextView
android:id="@+id/url"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:text="http://gdown.baidu.com/data/wisegame/038d77c9af95d110/jinritoutiao_627.apk"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toTopOf="@+id/guideline"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"/>
<com.daimajia.numberprogressbar.NumberProgressBar
android:id="@+id/probar"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toTopOf="@+id/guideline"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="@+id/url"/>
<android.support.constraint.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/guideline"
app:layout_constraintGuide_begin="297dp"
android:orientation="horizontal"/>
<android.support.constraint.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/guideline2"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.5"/>
</android.support.constraint.ConstraintLayout>
MainActivity里
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private TextView urlTv;
private NumberProgressBar proBar;
private int mCurrProgress = -1;
private boolean isDown;
private int mCruuDownsize = 0 ;
private File downApk;
private int fileSize;
private Handler mHandler=new Handler(){
@Override
public void handleMessage(Message msg) {
proBar.setProgress(mCurrProgress);
if(mCurrProgress==100) {
isDown = false;
mCruuDownsize=0;
fileSize=0;
mCurrProgress=-1;
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(downApk),
"application/vnd.android.package-archive");
startActivity(intent);
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
urlTv= (TextView) findViewById(url);
proBar= (NumberProgressBar) findViewById(R.id.probar);
downApk = new File(getExternalCacheDir(), "jinritoutiao.apk");
}
@Override
public void onClick(View v) {
if(v.getId()==R.id.start){
startDown();
}else if(v.getId()==R.id.pause){
stopDown();
}
}
private void stopDown() {
isDown=false;
}
private void startDown() {
if(isDown)
return;
Thread thread =new Thread(){
@Override
public void run() {
try {
URL url =new URL(urlTv.getText().toString());
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
int responseCode = 200;
if(fileSize!=0) {
conn.setRequestProperty("Range", "bytes=" + mCruuDownsize + "-" + fileSize);
responseCode=206;
}else{
fileSize=conn.getContentLength();
}
Log.e("run", "run: "+conn.getResponseCode());
if(conn.getResponseCode()==responseCode){
InputStream in = conn.getInputStream();
RandomAccessFile raf =new RandomAccessFile(downApk,"rw");
raf.seek(mCruuDownsize);
byte [] buff = new byte[1024];
int len =-1;
while(isDown && (len=in.read(buff))!=-1){
raf.write(buff,0,len);
mCruuDownsize+=len;
int pro = (int)(mCruuDownsize*100L/fileSize);
if(pro!=mCurrProgress){
mHandler.sendEmptyMessage(1);
mCurrProgress = pro;
}
}
raf.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
};
thread.start();
isDown= true;
}
}
AndroidManifest.xml设置权限
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>