多线程下载
新建下载Service:
public class DownloadService extends Service {
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public void onDestroy() {
super.onDestroy();
}
}
加入线程如下:
public class DownloadService extends Service implements Runnable{
//服务与线程的创建:通常服务在onCreate时创建线程,进行耗时操作的处理;
//每一次startService onStartCommand() 只是用于接收传递给线程的数据参数;
private Thread thread;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
//用于获取参数;交给线程执行
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onCreate() {
super.onCreate();
thread = new Thread(this);
}
public void run(){
//TODO: 处理文件下载
}
@Override
public void onDestroy() {
super.onDestroy();
thread.interrupt();
}
}
上面的
public class DownloadService extends Service implements Runnable{
//服务与线程的创建:通常服务在onCreate时创建线程,进行耗时操作的处理;
//每一次startService onStartCommand() 只是用于接收传递给线程的数据参数;
private Thread thread;
private LinkedList<String> queue;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
//用于获取参数;交给线程执行
if(intent!=null){
String url = intent.getStringExtra("url");
queue.add(url);
}
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onCreate() {
super.onCreate();
//用于缓冲onStartCommand 传递的参数;
queue = new LinkedList<String>();
thread = new Thread(this);
thread.start();
}
public void run(){
//TODO: 处理文件下载
while(true){
try {
Thread.sleep(1000L);
if(!queue.isEmpty()){
String s = queue.removeFirst();
Log.d("Download", "Download url=" + s);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
@Override
public void onDestroy() {
super.onDestroy();
thread.interrupt();
}
}
上面的方法可以用,但是稍微有点麻烦,+++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++++++++++++++
新建一个Service 在Other-》Service-》IntentService
或者自己写:
public class DownloadSingleService extends IntentService {
}
IntentService 如果是单独串行执行的话,就是用这个。
内置一个线程简化开发。
清单文件:
<service android:name=".service.DownloadSingleService"/>
添加:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Download URL"
android:onClick="btnDownloadUri"/>
public void btnDownloadUri(View view){
String url =textUri.getText().toString();
Intent intent = new Intent(this, DownloadSingleService.class);
intent.putExtra("url",url);
startService(intent);
}
修改DownloadSingle
注意一定要是空的构造方法。
-----
public class DownloadSingleService extends IntentService {
//构造方法,必须要调用父类的super(name) 这个构造;实际上没有什么作用,我们可以放一个空的
//因为清单文件注册Service 一定要有无参构造
//public DownloadSingleService(String name) {super("OnlyForDebug");}
public DownloadSingleService() {super("OnlyForDebug");}
/**
* onHandleIntent 实际上就是处理startService 传递的额Intent参数的方法
* 这个方法运行在子线程。
* @param intent
*/
@Override
protected void onHandleIntent(Intent intent) {
String url = intent.getStringExtra("url");
Log.d("DownloadSingleService", "开始下载 " + url);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Log.d("DownloadSingleService", "下载完成 " + url);
}
}
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
新建下载Service:
public class DownloadService extends Service {
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public void onDestroy() {
super.onDestroy();
}
}
加入线程如下:
public class DownloadService extends Service implements Runnable{
//服务与线程的创建:通常服务在onCreate时创建线程,进行耗时操作的处理;
//每一次startService onStartCommand() 只是用于接收传递给线程的数据参数;
private Thread thread;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
//用于获取参数;交给线程执行
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onCreate() {
super.onCreate();
thread = new Thread(this);
}
public void run(){
//TODO: 处理文件下载
}
@Override
public void onDestroy() {
super.onDestroy();
thread.interrupt();
}
}
上面的
public class DownloadService extends Service implements Runnable{
//服务与线程的创建:通常服务在onCreate时创建线程,进行耗时操作的处理;
//每一次startService onStartCommand() 只是用于接收传递给线程的数据参数;
private Thread thread;
private LinkedList<String> queue;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
//用于获取参数;交给线程执行
if(intent!=null){
String url = intent.getStringExtra("url");
queue.add(url);
}
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onCreate() {
super.onCreate();
//用于缓冲onStartCommand 传递的参数;
queue = new LinkedList<String>();
thread = new Thread(this);
thread.start();
}
public void run(){
//TODO: 处理文件下载
while(true){
try {
Thread.sleep(1000L);
if(!queue.isEmpty()){
String s = queue.removeFirst();
Log.d("Download", "Download url=" + s);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
@Override
public void onDestroy() {
super.onDestroy();
thread.interrupt();
}
}
上面的方法可以用,但是稍微有点麻烦,+++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++++++++++++++
新建一个Service 在Other-》Service-》IntentService
或者自己写:
public class DownloadSingleService extends IntentService {
}
IntentService 如果是单独串行执行的话,就是用这个。
内置一个线程简化开发。
清单文件:
<service android:name=".service.DownloadSingleService"/>
添加:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Download URL"
android:onClick="btnDownloadUri"/>
public void btnDownloadUri(View view){
String url =textUri.getText().toString();
Intent intent = new Intent(this, DownloadSingleService.class);
intent.putExtra("url",url);
startService(intent);
}
修改DownloadSingle
注意一定要是空的构造方法。
-----
public class DownloadSingleService extends IntentService {
//构造方法,必须要调用父类的super(name) 这个构造;实际上没有什么作用,我们可以放一个空的
//因为清单文件注册Service 一定要有无参构造
//public DownloadSingleService(String name) {super("OnlyForDebug");}
public DownloadSingleService() {super("OnlyForDebug");}
/**
* onHandleIntent 实际上就是处理startService 传递的额Intent参数的方法
* 这个方法运行在子线程。
* @param intent
*/
@Override
protected void onHandleIntent(Intent intent) {
String url = intent.getStringExtra("url");
Log.d("DownloadSingleService", "开始下载 " + url);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Log.d("DownloadSingleService", "下载完成 " + url);
}
}
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++