service下载任务

在service开启线程,利用接口更新进度

public class MainActivity extends AppCompatActivity {

    MyBindService msgService;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ImageView imageView = (ImageView) findViewById(R.id.image);

/** 进入Activity开始服务 */
        Intent intent = new Intent(MainActivity.this, MyBindService.class);
        bindService(intent, conn, Context.BIND_AUTO_CREATE);
    }

        ServiceConnection conn = new ServiceConnection() {
            @Override
            public void onServiceDisconnected(ComponentName name) {

            }

            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                Log.w("输出:", "onServiceConnected---------");
                //返回一个MsgService对象
                msgService = ((MyBindService.MsgBinder) service).getService();
                // 绑定成功后获取到服務开始下载

                msgService.startDownLoad();
                //注册回调接口来接收下载进度的变化
                msgService.setOnProgressListener(new MyBindService.OnProgressListener() {
                    @Override
                    public void onProgress(int progress) {
                        Log.w("输出:", "progress---------" + progress);
                        Log.w("输出:", "---thread----" + Thread.currentThread().getName());

                    }
                });

            }
        };

        protected void onDestroy() {
            super.onDestroy();
            this.unbindService(conn);
            Log.v("MainStadyServics", "out");
        }

            }

绑定service回调接口

public class MyBindService extends Service {
    /**
     * 进度条的最大值
     */
    public static final int MAX_PROGRESS = 100;
    /**
     * 进度条的进度值
     */
    private int progress = 0;

    /**
     * 更新进度的回调接口
     */
    private OnProgressListener onProgressListener;

    public interface OnProgressListener {
        void onProgress(int progress);
    }

    /**
     * 注册回调接口的方法,供外部调用
     *
     * @param onProgressListener
     */
    public void setOnProgressListener(OnProgressListener onProgressListener) {
        this.onProgressListener = onProgressListener;
    }

    /**
     * 增加get()方法,供Activity调用
     *
     * @return 下载进度
     */
    public int getProgress() {
        return progress;
    }

    /**
     * 模拟下载任务,每秒钟更新一次
     */
    public void startDownLoad() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (progress < MAX_PROGRESS) {
                    progress += 5;
                    //进度发生变化通知调用方,需要和切换到主线程
                    if (onProgressListener != null) {
                        Log.w("输出:", "---thread"+Thread.currentThread().getName());
                        onProgressListener.onProgress(progress);
                    }

                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                }
            }
        }).start();
    }


    /**
     * 返回一个Binder对象
     */
    @Override
    public IBinder onBind(Intent intent) {
        Log.w("TAG", "---onBind");
        return new MsgBinder();
    }

    //此方法是为了可以在Acitity中获得服务的实例
    public class MsgBinder extends Binder {
        /**
         * 获取当前Service的实例
         *
         * @return
         */
        public MyBindService getService() {
            return MyBindService.this;
        }
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.w("TAG", "---onCreate");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        /** 服务停止时 */
        Log.w("TAG", "---onDestroy");

    }


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.w("TAG", "---onStartCommand");
        return super.onStartCommand(intent, flags, startId);
    }

}

不要忘了注册服务

原文地址:https://www.cnblogs.com/Ocean123123/p/11021858.html