首先我们要集成greendao:
集成的过程不多说了,网上一大堆;
在就是建立我们的User类:
展示一下:
package com.example.greendaomoreloading;
import org.greenrobot.greendao.annotation.Entity;
import org.greenrobot.greendao.annotation.Id;
import org.greenrobot.greendao.annotation.Generated;
/**
* 作者:author
* 时间 :
* 说明:
*/
@Entity
public class User {
@Id
private Long id;
private Integer thread_id;
private Integer start_pos;
private Integer end_pos;
private Integer compelete_size;
private String url;
public String getUrl() {
return this.url;
}
public void setUrl(String url) {
this.url = url;
}
public Integer getCompelete_size() {
return this.compelete_size;
}
public void setCompelete_size(Integer compelete_size) {
this.compelete_size = compelete_size;
}
public Integer getEnd_pos() {
return this.end_pos;
}
public void setEnd_pos(Integer end_pos) {
this.end_pos = end_pos;
}
public Integer getStart_pos() {
return this.start_pos;
}
public void setStart_pos(Integer start_pos) {
this.start_pos = start_pos;
}
public Integer getThread_id() {
return this.thread_id;
}
public void setThread_id(Integer thread_id) {
this.thread_id = thread_id;
}
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
@Generated(hash = 2041931179)
public User(Long id, Integer thread_id, Integer start_pos, Integer end_pos,
Integer compelete_size, String url) {
this.id = id;
this.thread_id = thread_id;
this.start_pos = start_pos;
this.end_pos = end_pos;
this.compelete_size = compelete_size;
this.url = url;
}
@Generated(hash = 586692638)
public User() {
}
}
通过greendao来实现断点的思想:
参考地址:https://www.cnblogs.com/cainiaodongdong/p/7880723.html
通过参考的文章,我们要在清单文件里面添加一些设置:
权限和name:
展示一下清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.greendaomoreloading">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<application
android:name=".App" android:allowBackup="true" android:icon="@mipmap/ic_launcher"
android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true" 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>
最后我们将布局和MainActivity展示:
布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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.greendaomoreloading.MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="23dp"
android:layout_marginStart="23dp"
android:layout_marginTop="18dp"
android:text="TextView" />
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="7.5dp"
android:layout_below="@+id/textView"
android:layout_marginRight="8dp"
android:max="100"
android:progress="100"
android:visibility="visible" />
<VideoView
android:layout_width="match_parent"
android:layout_height="300dp"
android:id="@+id/vv"
android:layout_below="@+id/progressBar"
android:layout_marginTop="50dp"/>
<Button
android:id="@+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toEndOf="@+id/textView"
android:layout_toRightOf="@+id/textView"
android:text="下载" />
<Button
android:id="@+id/delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginLeft="48dp"
android:layout_marginStart="48dp"
android:layout_toEndOf="@+id/start"
android:layout_toRightOf="@+id/start"
android:text="暂停" />
</RelativeLayout>
MainActivity:
package com.example.greendaomoreloading;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.VideoView;
public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
private ProgressBar mProgressBar;
private Button start;
private Button pause;
private TextView total;
private int max;
private DownloadUtil mDownloadUtil;
private VideoView vv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
vv= (VideoView) findViewById(R.id.vv);
total = (TextView) findViewById(R.id.textView);
start = (Button) findViewById(R.id.start);
pause = (Button) findViewById(R.id.delete);
mProgressBar = (ProgressBar) findViewById(R.id.progressBar);
String urlString = "http://2449.vod.myqcloud.com/2449_22ca37a6ea9011e5acaaf51d105342e3.f20.mp4";
String localPath = Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/local2";
mDownloadUtil = new DownloadUtil(2, localPath, "adc.mp4", urlString,
this);
mDownloadUtil.setOnDownloadListener(new DownloadUtil.OnDownloadListener() {
@Override
public void downloadStart(int fileSize) {
// TODO Auto-generated method stub
Log.w(TAG, "fileSize::" + fileSize);
max = fileSize;
mProgressBar.setMax(fileSize);
}
@Override
public void downloadProgress(int downloadedSize) {
// TODO Auto-generated method stub
Log.w(TAG, "Compelete::" + downloadedSize);
mProgressBar.setProgress(downloadedSize);
total.setText((int) downloadedSize * 100 / max + "%");
}
@Override
public void downloadEnd() {
vv.setVideoPath(Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/local2/adc.mp4");
vv.start();
}
});
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
mDownloadUtil.start();
}
});
pause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
mDownloadUtil.pause();
}
});
}
}
网友评论