Android使用OKHttp3实现下载(断点续传、显示运行进度)
OKHttp3是现在很流行的Android网络请求框架,那么怎样利用Android实现断点续传呢,今天写了个Demo尝试了一下,感觉还是有点意思
准备阶段
我们会用到OKHttp3来做网络请求,使用RxJava来实现线程的切换,并且开启Java8来启用Lambda表达式,毕竟RxJava实现线程切换很方便,并且数据流的形式也很舒服,同一时候Lambda和RxJava配合食用味道更佳
打开我们的app Module下的build.gradle,代码例如以下
apply plugin: 'com.android.application'
android {
    compileSdkVersion 24
    buildToolsVersion "24.0.3"
    defaultConfig {
        applicationId "com.lanou3g.downdemo"
        minSdkVersion 15
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        //为了开启Java8
        jackOptions{
            enabled true;
        }
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    //开启Java1.8 可以使用lambda表达式
    compileOptions{
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:24.1.1'
    testCompile 'junit:junit:4.12'
    //OKHttp
    compile 'com.squareup.okhttp3:okhttp:3.6.0'
    //RxJava和RxAndroid 用来做线程切换的
    compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
    compile 'io.reactivex.rxjava2:rxjava:2.0.1'
}
OKHttp和RxJava,RxAndroid使用的都是最新的版本号,而且配置开启了Java8
布局文件
接着開始书写布局文件
<?xml version="1.0" encoding="utf-8"? >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.lanou3g.downdemo.MainActivity"> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ProgressBar
android:id="@+id/main_progress1"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
style="@style/Widget.AppCompat.ProgressBar.Horizontal" />
<Button
android:id="@+id/main_btn_down1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下载1"/>
<Button
android:id="@+id/main_btn_cancel1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="取消1"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ProgressBar
android:id="@+id/main_progress2"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
style="@style/Widget.AppCompat.ProgressBar.Horizontal" />
<Button
android:id="@+id/main_btn_down2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下载2"/>
<Button
android:id="@+id/main_btn_cancel2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="取消2"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ProgressBar
android:id="@+id/main_progress3"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
style="@style/Widget.AppCompat.ProgressBar.Horizontal" />
<Button
android:id="@+id/main_btn_down3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下载3"/>
<Button
android:id="@+id/main_btn_cancel3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="取消3"/>
</LinearLayout>
</LinearLayout>
大概是这个样子的
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY2Z5MTM3MDAw/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">
3个ProgressBar就是为了显示运行进度的,每一个ProgressBar相应2个Button,一个是開始下载,一个是暂停(取消)下载,这里须要说明的是,对下载来说暂停和取消没有什么差别,除非当取消的时候,会顺带把暂时文件都删除了,在本例里是不区分他俩的.
Application
package com.lanou3g.downdemo; import android.app.Application;
import android.content.Context; /**
* Created by 陈丰尧 on 2017/2/2.
*/ public class MyApp extends Application {
public static Context sContext;//全局的Context对象 @Override
public void onCreate() {
super.onCreate();
sContext = this;
}
}
能够看到,我们就是要获得一个全局的Context对象的
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.lanou3g.downdemo"> <!--网络权限-->
<uses-permission android:name="android.permission.INTERNET"/> <application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:name=".MyApp"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application> </manifest>
我们仅仅须要一个网络权限,在application标签下,加入name属性,来指向我们的Application
DownloadManager
package com.lanou3g.downdemo; import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.concurrent.atomic.AtomicReference; import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response; /**
* Created by 陈丰尧 on 2017/2/2.
*/ public class DownloadManager { private static final AtomicReference<DownloadManager> INSTANCE = new AtomicReference<>();
private HashMap<String, Call> downCalls;//用来存放各个下载的请求
private OkHttpClient mClient;//OKHttpClient; //获得一个单例类
public static DownloadManager getInstance() {
for (; ; ) {
DownloadManager current = INSTANCE.get();
if (current != null) {
return current;
}
current = new DownloadManager();
if (INSTANCE.compareAndSet(null, current)) {
return current;
}
}
} private DownloadManager() {
downCalls = new HashMap<>();
mClient = new OkHttpClient.Builder().build();
} /**
* 開始下载
*
* @param url 下载请求的网址
* @param downLoadObserver 用来回调的接口
*/
public void download(String url, DownLoadObserver downLoadObserver) {
Observable.just(url)
.filter(s -> !downCalls.containsKey(s))//call的map已经有了,就证明正在下载,则这次不下载
.flatMap(s -> Observable.just(createDownInfo(s)))
.map(this::getRealFileName)//检測本地目录,生成新的文件名称
.flatMap(downloadInfo -> Observable.create(new DownloadSubscribe(downloadInfo)))//下载
.observeOn(AndroidSchedulers.mainThread())//在主线程回调
.subscribeOn(Schedulers.io())//在子线程运行
.subscribe(downLoadObserver);//加入观察者 } public void cancel(String url) {
Call call = downCalls.get(url);
if (call != null) {
call.cancel();//取消
}
downCalls.remove(url);
} /**
* 创建DownInfo
*
* @param url 请求网址
* @return DownInfo
*/
private DownloadInfo createDownInfo(String url) {
DownloadInfo downloadInfo = new DownloadInfo(url);
long contentLength = getContentLength(url);//获得文件大小
downloadInfo.setTotal(contentLength);
String fileName = url.substring(url.lastIndexOf("/"));
downloadInfo.setFileName(fileName);
return downloadInfo;
} private DownloadInfo getRealFileName(DownloadInfo downloadInfo) {
String fileName = downloadInfo.getFileName();
long downloadLength = 0, contentLength = downloadInfo.getTotal();
File file = new File(MyApp.sContext.getFilesDir(), fileName);
if (file.exists()) {
//找到了文件,代表已经下载过,则获取其长度
downloadLength = file.length();
}
//之前下载过,须要又一次来一个文件
int i = 1;
while (downloadLength >= contentLength) {
int dotIndex = fileName.lastIndexOf(".");
String fileNameOther;
if (dotIndex == -1) {
fileNameOther = fileName + "(" + i + ")";
} else {
fileNameOther = fileName.substring(0, dotIndex)
+ "(" + i + ")" + fileName.substring(dotIndex);
}
File newFile = new File(MyApp.sContext.getFilesDir(), fileNameOther);
file = newFile;
downloadLength = newFile.length();
i++;
}
//设置改变过的文件名称/大小
downloadInfo.setProgress(downloadLength);
downloadInfo.setFileName(file.getName());
return downloadInfo;
} private class DownloadSubscribe implements ObservableOnSubscribe<DownloadInfo> {
private DownloadInfo downloadInfo; public DownloadSubscribe(DownloadInfo downloadInfo) {
this.downloadInfo = downloadInfo;
} @Override
public void subscribe(ObservableEmitter<DownloadInfo> e) throws Exception {
String url = downloadInfo.getUrl();
long downloadLength = downloadInfo.getProgress();//已经下载好的长度
long contentLength = downloadInfo.getTotal();//文件的总长度
//初始进度信息
e.onNext(downloadInfo); Request request = new Request.Builder()
//确定下载的范围,加入此头,则服务器就能够跳过已经下载好的部分
.addHeader("RANGE", "bytes=" + downloadLength + "-" + contentLength)
.url(url)
.build();
Call call = mClient.newCall(request);
downCalls.put(url, call);//把这个加入到call里,方便取消
Response response = call.execute(); File file = new File(MyApp.sContext.getFilesDir(), downloadInfo.getFileName());
InputStream is = null;
FileOutputStream fileOutputStream = null;
try {
is = response.body().byteStream();
fileOutputStream = new FileOutputStream(file, true);
byte[] buffer = new byte[2048];//缓冲数组2kB
int len;
while ((len = is.read(buffer)) != -1) {
fileOutputStream.write(buffer, 0, len);
downloadLength += len;
downloadInfo.setProgress(downloadLength);
e.onNext(downloadInfo);
}
fileOutputStream.flush();
downCalls.remove(url);
} finally {
//关闭IO流
IOUtil.closeAll(is, fileOutputStream); }
e.onComplete();//完毕
}
} /**
* 获取下载长度
*
* @param downloadUrl
* @return
*/
private long getContentLength(String downloadUrl) {
Request request = new Request.Builder()
.url(downloadUrl)
.build();
try {
Response response = mClient.newCall(request).execute();
if (response != null && response.isSuccessful()) {
long contentLength = response.body().contentLength();
response.close();
return contentLength == 0 ? DownloadInfo.TOTAL_ERROR : contentLength;
}
} catch (IOException e) {
e.printStackTrace();
}
return DownloadInfo.TOTAL_ERROR;
} }
package com.lanou3g.downdemo; import io.reactivex.Observer;
import io.reactivex.disposables.Disposable; /**
* Created by 陈丰尧 on 2017/2/2.
*/ public abstract class DownLoadObserver implements Observer<DownloadInfo> {
protected Disposable d;//能够用于取消注冊的监听者
protected DownloadInfo downloadInfo;
@Override
public void onSubscribe(Disposable d) {
this.d = d;
} @Override
public void onNext(DownloadInfo downloadInfo) {
this.downloadInfo = downloadInfo;
} @Override
public void onError(Throwable e) {
e.printStackTrace();
} }
在RxJava2中 这个Observer有点变化,当注冊观察者的时候,会调用onSubscribe方法,而该方法參数就是用来取消注冊的,这种修改能够更灵活的有监听者来取消监听了,我们的进度信息会一直的传送的onNext方法里,这里将下载所须要的内容封了一个类叫DownloadInfo
package com.lanou3g.downdemo; /**
* Created by 陈丰尧 on 2017/2/2.
* 下载信息
*/ public class DownloadInfo {
public static final long TOTAL_ERROR = -1;//获取进度失败
private String url;
private long total;
private long progress;
private String fileName; public DownloadInfo(String url) {
this.url = url;
} public String getUrl() {
return url;
} public String getFileName() {
return fileName;
} public void setFileName(String fileName) {
this.fileName = fileName;
} public long getTotal() {
return total;
} public void setTotal(long total) {
this.total = total;
} public long getProgress() {
return progress;
} public void setProgress(long progress) {
this.progress = progress;
}
}
这个类就是一些基本信息,total就是须要下载的文件的总大小,而progress就是当前下载的进度了,这样就能够计算出下载的进度信息了
/**
* 获取下载长度
*
* @param downloadUrl
* @return
*/
private long getContentLength(String downloadUrl) {
Request request = new Request.Builder()
.url(downloadUrl)
.build();
try {
Response response = mClient.newCall(request).execute();
if (response != null && response.isSuccessful()) {
long contentLength = response.body().contentLength();
response.close();
return contentLength == 0 ? DownloadInfo.TOTAL_ERROR : contentLength;
}
} catch (IOException e) {
e.printStackTrace();
}
return DownloadInfo.TOTAL_ERROR;
}
能够看到,事实上就是在通过OK进行了一次网络请求,而且从返回的头信息里拿到文件的大小信息,一般这个信息都是能够拿到的,除非下载网址不是直接指向资源文件的,而是自己手写的Servlet,那就得跟后台人员沟通好了.注意,这次网络请求并没有真正的去下载文件,而是请求个大小就结束了,详细原因会在后面真正请求数据的时候解释
获取完文件大小后,就能够去硬盘里找文件了,这里调用了getRealFileName方法
private DownloadInfo getRealFileName(DownloadInfo downloadInfo) {
        String fileName = downloadInfo.getFileName();
        long downloadLength = 0, contentLength = downloadInfo.getTotal();
        File file = new File(MyApp.sContext.getFilesDir(), fileName);
        if (file.exists()) {
            //找到了文件,代表已经下载过,则获取其长度
            downloadLength = file.length();
        }
        //之前下载过,须要又一次来一个文件
        int i = 1;
        while (downloadLength >= contentLength) {
            int dotIndex = fileName.lastIndexOf(".");
            String fileNameOther;
            if (dotIndex == -1) {
                fileNameOther = fileName + "(" + i + ")";
            } else {
                fileNameOther = fileName.substring(0, dotIndex)
                        + "(" + i + ")" + fileName.substring(dotIndex);
            }
            File newFile = new File(MyApp.sContext.getFilesDir(), fileNameOther);
            file = newFile;
            downloadLength = newFile.length();
            i++;
        }
        //设置改变过的文件名称/大小
        downloadInfo.setProgress(downloadLength);
        downloadInfo.setFileName(file.getName());
        return downloadInfo;
    }
这种方法就是看本地是否有已经下载过的文件,假设有,再推断一次本地文件的大小和server上数据的大小,假设是一样的,证明之前下载全了,就再成一个带(1)这种文件,而假设本地文件大小比server上的小的话,那么证明之前下载了一半断掉了,那么就把进度信息保存上,并把文件名称也存上,看完了再回到download方法
之后就開始真正的网络请求了,这里写了一个内部类来实现ObservableOnSubscribe接口,这个接口也是RxJava2的,东西和之前一样,好像仅仅改了名字,看一下代码
private class DownloadSubscribe implements ObservableOnSubscribe<DownloadInfo> {
        private DownloadInfo downloadInfo;
        public DownloadSubscribe(DownloadInfo downloadInfo) {
            this.downloadInfo = downloadInfo;
        }
        @Override
        public void subscribe(ObservableEmitter<DownloadInfo> e) throws Exception {
            String url = downloadInfo.getUrl();
            long downloadLength = downloadInfo.getProgress();//已经下载好的长度
            long contentLength = downloadInfo.getTotal();//文件的总长度
            //初始进度信息
            e.onNext(downloadInfo);
            Request request = new Request.Builder()
                    //确定下载的范围,加入此头,则server就能够跳过已经下载好的部分
                    .addHeader("RANGE", "bytes=" + downloadLength + "-" + contentLength)
                    .url(url)
                    .build();
            Call call = mClient.newCall(request);
            downCalls.put(url, call);//把这个加入到call里,方便取消
            Response response = call.execute();
            File file = new File(MyApp.sContext.getFilesDir(), downloadInfo.getFileName());
            InputStream is = null;
            FileOutputStream fileOutputStream = null;
            try {
                is = response.body().byteStream();
                fileOutputStream = new FileOutputStream(file, true);
                byte[] buffer = new byte[2048];//缓冲数组2kB
                int len;
                while ((len = is.read(buffer)) != -1) {
                    fileOutputStream.write(buffer, 0, len);
                    downloadLength += len;
                    downloadInfo.setProgress(downloadLength);
                    e.onNext(downloadInfo);
                }
                fileOutputStream.flush();
                downCalls.remove(url);
            } finally {
                //关闭IO流
                IOUtil.closeAll(is, fileOutputStream);
            }
            e.onComplete();//完毕
        }
    }
主要看subscribe方法
首先拿到url,当前进度信息和文件的总大小,然后開始网络请求,注意这次网络请求的时候须要加入一条头信息
.addHeader("RANGE", "bytes=" + downloadLength + "-" + contentLength)
这条头信息的意思是下载的范围是多少,downloadLength是从哪開始下载,contentLength是下载到哪,当要断点续传的话必须加入这个头,让输入流跳过多少字节的形式是不行的,所以我们要想能成功的加入这条信息那么就必须对这个url请求2次,一次拿到总长度,来方便推断本地是否有下载一半的数据,第二次才開始真正的读流进行网络请求,我还想了一种思路,当文件没有下载完毕的时候加入一个自己定义的后缀,当下载完毕再把这个后缀取消了,应该就不须要请求两次了.
接下来就是正常的网络请求,向本地写文件了,而写文件到本地这,网上大多用的是RandomAccessFile这个类,可是假设不涉及到多个部分拼接的话是不是必需的,直接使用输出流就好了,在输出流的构造方法上加入一个true的參数,代表是在原文件的后面加入数据就可以,而在循环里,不断的调用onNext方法发送进度信息,当写完了之后别忘了关流,同一时候把call对象从hashMap中移除了.这里写了一个IOUtil来关流
package com.lanou3g.downdemo; import java.io.Closeable;
import java.io.IOException; /**
* Created by 陈丰尧 on 2017/2/2.
*/ public class IOUtil {
public static void closeAll(Closeable... closeables){
if(closeables == null){
return;
}
for (Closeable closeable : closeables) {
if(closeable!=null){
try {
closeable.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
事实上就是挨一个推断是否为空,并关闭罢了
这样download方法就完毕了,剩下的就是切换线程,注冊观察者了
MainActivity
package com.lanou3g.downdemo; import android.net.Uri;
import android.support.annotation.IdRes;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.Toast; public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button downloadBtn1, downloadBtn2, downloadBtn3;
private Button cancelBtn1, cancelBtn2, cancelBtn3;
private ProgressBar progress1, progress2, progress3;
private String url1 = "http://192.168.31.169:8080/out/dream.flac";
private String url2 = "http://192.168.31.169:8080/out/music.mp3";
private String url3 = "http://192.168.31.169:8080/out/code.zip";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); downloadBtn1 = bindView(R.id.main_btn_down1);
downloadBtn2 = bindView(R.id.main_btn_down2);
downloadBtn3 = bindView(R.id.main_btn_down3); cancelBtn1 = bindView(R.id.main_btn_cancel1);
cancelBtn2 = bindView(R.id.main_btn_cancel2);
cancelBtn3 = bindView(R.id.main_btn_cancel3); progress1 = bindView(R.id.main_progress1);
progress2 = bindView(R.id.main_progress2);
progress3 = bindView(R.id.main_progress3); downloadBtn1.setOnClickListener(this);
downloadBtn2.setOnClickListener(this);
downloadBtn3.setOnClickListener(this); cancelBtn1.setOnClickListener(this);
cancelBtn2.setOnClickListener(this);
cancelBtn3.setOnClickListener(this);
} @Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.main_btn_down1:
DownloadManager.getInstance().download(url1, new DownLoadObserver() {
@Override
public void onNext(DownloadInfo value) {
super.onNext(value);
progress1.setMax((int) value.getTotal());
progress1.setProgress((int) value.getProgress());
} @Override
public void onComplete() {
if(downloadInfo != null){
Toast.makeText(MainActivity.this,
downloadInfo.getFileName() + "-DownloadComplete",
Toast.LENGTH_SHORT).show();
}
}
});
break;
case R.id.main_btn_down2:
DownloadManager.getInstance().download(url2, new DownLoadObserver() {
@Override
public void onNext(DownloadInfo value) {
super.onNext(value);
progress2.setMax((int) value.getTotal());
progress2.setProgress((int) value.getProgress());
} @Override
public void onComplete() {
if(downloadInfo != null){
Toast.makeText(MainActivity.this,
downloadInfo.getFileName() + Uri.encode("下载完毕"),
Toast.LENGTH_SHORT).show();
}
}
});
break;
case R.id.main_btn_down3:
DownloadManager.getInstance().download(url3, new DownLoadObserver() {
@Override
public void onNext(DownloadInfo value) {
super.onNext(value);
progress3.setMax((int) value.getTotal());
progress3.setProgress((int) value.getProgress());
} @Override
public void onComplete() {
if(downloadInfo != null){
Toast.makeText(MainActivity.this,
downloadInfo.getFileName() + "下载完毕",
Toast.LENGTH_SHORT).show();
}
}
});
break;
case R.id.main_btn_cancel1:
DownloadManager.getInstance().cancel(url1);
break;
case R.id.main_btn_cancel2:
DownloadManager.getInstance().cancel(url2);
break;
case R.id.main_btn_cancel3:
DownloadManager.getInstance().cancel(url3);
break;
}
} private <T extends View> T bindView(@IdRes int id){
View viewById = findViewById(id);
return (T) viewById;
}
}
Activity里没什么了,就是注冊监听,開始下载,取消下载这些了,以下我们来看看效果吧
执行效果
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY2Z5MTM3MDAw/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">
能够看到 多个下载,断点续传什么的都已经成功了,最后我的文件网址是我自己的局域网,大家写的时候别忘了换了..
Android使用OKHttp3实现下载(断点续传、显示运行进度)的更多相关文章
- Android学习笔记(八)——显示运行进度对话框
		
显示运行进度对话框 我们经常有这种经历:运行某一应用程序时.须要等待一会,这时会显示一个进度(Please Wait)对话框,让用户知道操作正在进行. 我们继续在上一篇中的程序中加入代码~ 1.在上一 ...
 - Android 网络交互之下载断点续传
		
一.概述 1.概念 断点续传主要用于下载,本文也主要讲述下载时的断点续传的逻辑思路.顾名思义,断点续传就是下载从中断的地方继续下载,一般是因为暂停或者网络故障导致的下载中断,当恢复下载的时候可以从已经 ...
 - android——TextView默认文字开发时显示运行时隐藏
		
根布局添加属性: xmlns:tools="http://schemas.android.com/tools" textview添加属性: tools:text="默认文 ...
 - Android开发之多线程下载、断点续传、进度条和文本显示
		
代码实现了在Android环境下的多线程下载.断点续传.进度条显示和文本显示百分数: import java.io.BufferedReader; import java.io.File; impor ...
 - 解决Android SDK Manager 更新下载慢以及待安装包列表不显示
		
问题描述: Android SDK Manager 无法下载更新,或者更新速度超慢,或者待安装包列表不显示 解决方法: 第一步:修改hosts文件 修改后的hosts 文件内容为: 127.0.0.1 ...
 - Android 下载文件 显示进度条
		
加入两个权限 一个是联网,另一个是读写SD卡 <uses-permission android:name="android.permission.INTERNET">& ...
 - Android 使用 DownloadManager 管理系统下载任务的方法,android管理系统
		
从Android 2.3(API level 9)开始Android用系统服务(Service)的方式提供了Download Manager来优化处理长时间的下载操作.Download Manager ...
 - Android 使用 DownloadManager 管理系统下载任务的方法
		
在红黑联盟上看到的.这几天一直考虑做一个Notification 的带下载功能的自己定义通知.但没搞出来.无意中在论坛看到这个.先Mark,明天试试. 从Android 2.3(API level 9 ...
 - Android Studio导入github下载的project和module
		
前言:我们以前eclispe时代, 经常都是跑到github浏览第三方开源资源,然后下载下来,运行一下sample之类的,学习没有接触的第三方安卓库,但是到了Android Studio,在githu ...
 
随机推荐
- 将数据缓存到sessionStorage中
			
//获取侧边栏 if (sessionStorage.getItem(`${env}${empId}leftMenu`)) { const leftMenu = JSON.parse(sessionS ...
 - EPEL 安装源
			
EPEL 安装源 EPEL 是 Extra Packages for Enterprise Linux 的缩写(EPEL),是用于 Fedora-based Red Hat Enterprise Li ...
 - caffe+Ubuntu14.04.10 +cuda7.0/7.5+CuDNNv4 安装
			
特别说明: Caffe 官网地址:http://caffe.berkeleyvision.org/ 本文为作者亲自实验完成,但仅限用于学术交流使用,使用本指南造成的任何不良后果由使用者自行承担,与本文 ...
 - 【bzoj3585】mex 线段树 mex,sg
			
Description 有一个长度为n的数组{a1,a2,…,an}.m次询问,每次询问一个区间内最小没有出现过的自然数. Input 第一行n,m. 第二行为n个数. 从第三行开始,每行一个询问l, ...
 - BZOJ 2728: [HNOI2012]与非
			
2728: [HNOI2012]与非 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 786 Solved: 371[Submit][Status][ ...
 - 【BZOJ4481&JSOI2015】非诚勿扰(数学期望)
			
听说JSOI有版权问题就不放图了 如果前面的文章里的图需要删掉请通知我 题意:有一些女的要挑一些男的,挑中的几率均为p.一个男的可以无限次被挑中.若女a选中男b,女c选中男d,a<c,b> ...
 - javascript实现htmlEncode与htmlDecode
			
原文发布时间为:2011-04-19 -- 来源于本人的百度文章 [由搬家工具导入] htmlencode with javascript function htmlEncode(html) { ...
 - Repeater用ul li,一行显示多条数据
			
原文发布时间为:2009-08-26 -- 来源于本人的百度文章 [由搬家工具导入] .rep { width:680px; float:left; l ...
 - TextBox取不到值及其TextBox取不到js赋的值
			
原文发布时间为:2009-10-22 -- 来源于本人的百度文章 [由搬家工具导入] 原因:使用了一个只读的TextBox控件 曾经遇到过这样的问题:使用了一个只读的TextBox控件,但是在后台代码 ...
 - 用dataset做数据源时,让gridview显示的列名与数据库表中的字段名不同
			
原文发布时间为:2008-10-27 -- 来源于本人的百度文章 [由搬家工具导入] 确定GridView的AutoGenerateColumns设置为False;使用GridView的“编辑列”,添 ...