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

从来好事天生俭,自古瓜儿苦后甜。这篇文章主要讲述Android学习笔记---使用Service模仿下载效果相关的知识,希望能为你提供帮助。
今天学了Service,做了一个进度条的效果,和大家分享一下,
来贴一下布局文件

1 < ?xml version="1.0" encoding="utf-8"?> 2 < LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3xmlns:tools="http://schemas.android.com/tools" 4android:layout_width="match_parent" 5android:layout_height="match_parent" 6android:orientation="vertical" 7tools:context="com.wuxianedu.serviceoractivity.MainActivity"> 8 9< ProgressBar android:id="@+id/jindu" 10android:layout_width="match_parent" 11android:layout_height="wrap_content" 12style="?android:attr/progressBarStyleHorizontal" 13/> 14< Button android:id="@+id/wo" 15android:layout_width="wrap_content" 16android:layout_height="wrap_content" 17android: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类 15private ServiceConnection serviceConnection 16; 17private ProgressBar pop; 18 19@Override 20protected void onCreate(Bundle savedInstanceState) { 21super.onCreate(savedInstanceState); 22setContentView(R.layout.activity_main); 23pop = (ProgressBar) findViewById(R.id.jindu); 24findViewById(R.id.wo).setOnClickListener(this); 25 26} 27 28@Override 29public void onClick(View v) { 30switch (v.getId()){ 31case R.id.wo: 32//new了一个serviceConnection的回调事件, 33if(serviceConnection == null){ 34serviceConnection = new ServiceConnection() { 35@Override 36public void onServiceConnected(ComponentName name, IBinder service) { 37//把Service里面的内部类堕胎为子类,来使用里面的方法 38final MyService.Myhui myhui = (MyService.Myhui) service; 39//参数为new出来静态接口 40myhui.startDownload(new MyService.OnProgressChangeListener() { 41@Override 42public void onProgressChange(final int currentProgress) { 43//为进度条赋值 44pop.setMax(MyService.MAX); 45//多线程 46runOnUiThread(new Runnable() { 47@Override 48public void run() { 49//为进度条赋值 50pop.setProgress(currentProgress); 51//判断当前值是否大于MAX,如果大于就是下载完了, 52if(currentProgress > = MyService.MAX){ 53Toast.makeText(MainActivity.this, "下载完成!", Toast.LENGTH_SHORT).show(); 54} 55} 56}); 57 58} 59}); 60} 61@Override 62public void onServiceDisconnected(ComponentName name) { 63// Toast.makeText(MainActivity.this, "下载完成!", Toast.LENGTH_SHORT).show(); 64} 65}; 66} 67//启动Service服务 68bindService(new Intent(this,MyService.class),serviceConnection,BIND_AUTO_CREATE); 69break; 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 { 14private int currentProgress; 15public static int MAX = 100; 16 17@Nullable 18//回调事件 19@Override 20public IBinder onBind(Intent intent) { 21return new Myhui(); 22} 23 24class Myhui extends Binder{ 25 26/*public MyService getService(){ 27return MyService.this; 28}*/ 29public void startDownload (final OnProgressChangeListener onProgressChangeListener){ 30new Thread(new Runnable() { 31@Override 32public void run() { 33while (true){ 34currentProgress += 5; 35Log.d("main","=================="+currentProgress+""); 36//每次给接口里面的方法赋值 37onProgressChangeListener.onProgressChange(currentProgress); 38try { 39Thread.sleep(1000L); 40} catch (InterruptedException e) { 41e.printStackTrace(); 42} 43//判断当前值是否大于MAX最大值,如果大于跳出循环 44if(currentProgress> =MAX){ 45break; 46} 47} 48} 49}).start(); 50} 51} 52//获取当前的值 53interface OnProgressChangeListener { 54void onProgressChange(int currentProgress); 55} 56 }

 
代码就这些,注释里面写的非常清楚,大家看一下就能看懂啦
下面附上代码下载地址:明天早上附上,网速太慢了
【Android学习笔记---使用Service模仿下载效果】 

    推荐阅读