Android学习笔记---使用Service模仿下载效果

今天学了Service,做了一个进度条的效果,和大家分享一下,

来贴一下布局文件

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:tools="http://schemas.android.com/tools"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:orientation="vertical"
 7     tools:context="com.wuxianedu.serviceoractivity.MainActivity">
 8 
 9     <ProgressBar android:id="@+id/jindu"
10         android:layout_width="match_parent"
11         android:layout_height="wrap_content"
12         style="?android:attr/progressBarStyleHorizontal"
13         />
14     <Button android:id="@+id/wo"
15         android:layout_width="wrap_content"
16         android:layout_height="wrap_content"
17         android:text="点我下载"/>
18 
19 </LinearLayout>

MainActivity.java

 1 package com.wuxianedu.serviceoractivity;
 2 
 3 import android.content.ComponentName;
 4 import android.content.Intent;
 5 import android.content.ServiceConnection;
 6 import android.os.IBinder;
 7 import android.support.v7.app.AppCompatActivity;
 8 import android.os.Bundle;
 9 import android.view.View;
10 import android.widget.ProgressBar;
11 import android.widget.Toast;
12 
13 public class MainActivity extends AppCompatActivity implements View.OnClickListener {
14     //new一个启动Service类
15     private ServiceConnection serviceConnection
16             ;
17     private ProgressBar pop;
18 
19     @Override
20     protected void onCreate(Bundle savedInstanceState) {
21         super.onCreate(savedInstanceState);
22         setContentView(R.layout.activity_main);
23          pop = (ProgressBar) findViewById(R.id.jindu);
24         findViewById(R.id.wo).setOnClickListener(this);
25 
26     }
27 
28     @Override
29     public void onClick(View v) {
30         switch (v.getId()){
31             case R.id.wo:
32                 //new了一个serviceConnection的回调事件,
33                 if(serviceConnection == null){
34                     serviceConnection = new ServiceConnection() {
35                         @Override
36                         public void onServiceConnected(ComponentName name, IBinder service) {
37                                     //把Service里面的内部类堕胎为子类,来使用里面的方法
38                             final MyService.Myhui myhui = (MyService.Myhui) service;
39                                     //参数为new出来静态接口
40                                 myhui.startDownload(new MyService.OnProgressChangeListener() {
41                                     @Override
42                                     public void onProgressChange(final int currentProgress) {
43                                         //为进度条赋值
44                                         pop.setMax(MyService.MAX);
45                                         //多线程
46                                         runOnUiThread(new Runnable() {
47                                             @Override
48                                             public void run() {
49                                                 //为进度条赋值
50                                                 pop.setProgress(currentProgress);
51                                                 //判断当前值是否大于MAX,如果大于就是下载完了,
52                                                 if(currentProgress >= MyService.MAX){
53                                                     Toast.makeText(MainActivity.this, "下载完成!", Toast.LENGTH_SHORT).show();
54                                                 }
55                                             }
56                                         });
57 
58                                     }
59                                 });
60                         }
61                         @Override
62                         public void onServiceDisconnected(ComponentName name) {
63                            // Toast.makeText(MainActivity.this, "下载完成!", Toast.LENGTH_SHORT).show();
64                         }
65                     };
66                 }
67                 //启动Service服务
68                 bindService(new Intent(this,MyService.class),serviceConnection,BIND_AUTO_CREATE);
69                 break;
70         }
71     };
72 }

MyService.java

 1 package com.wuxianedu.serviceoractivity;
 2 
 3 import android.app.Service;
 4 import android.content.Intent;
 5 import android.os.Binder;
 6 import android.os.IBinder;
 7 import android.support.annotation.Nullable;
 8 import android.util.Log;
 9 
10 /**
11  * Created by Administrator on 2016/9/23.
12  */
13 public class MyService extends Service {
14     private int currentProgress;
15     public static int MAX = 100;
16 
17     @Nullable
18     //回调事件
19     @Override
20     public IBinder onBind(Intent intent) {
21         return new Myhui();
22     }
23 
24     class Myhui extends Binder{
25 
26         /*public MyService getService(){
27             return MyService.this;
28         }*/
29         public void startDownload (final OnProgressChangeListener onProgressChangeListener){
30             new Thread(new Runnable() {
31                 @Override
32                 public void run() {
33                     while (true){
34                         currentProgress += 5;
35                         Log.d("main","=================="+currentProgress+"");
36                         //每次给接口里面的方法赋值
37                         onProgressChangeListener.onProgressChange(currentProgress);
38                         try {
39                             Thread.sleep(1000L);
40                         } catch (InterruptedException e) {
41                             e.printStackTrace();
42                         }
43                         //判断当前值是否大于MAX最大值,如果大于跳出循环
44                        if(currentProgress>=MAX){
45                             break;
46                         }
47                     }
48                 }
49             }).start();
50         }
51     }
52     //获取当前的值
53     interface OnProgressChangeListener {
54         void onProgressChange(int currentProgress);
55     }
56 }

代码就这些,注释里面写的非常清楚,大家看一下就能看懂啦

下面附上代码下载地址:明天早上附上,网速太慢了

原文地址:https://www.cnblogs.com/langfei8818/p/5901772.html