bindservice与Activity通信

 

package com.example.jikangwang.myapplication;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.button)
    Button button;

    @BindView(R.id.progressbar)
    ProgressBar progressBar;
    Handler mHandler;

    volatile boolean flag=true;
    Object object=new Object();


    ServiceConnection serviceConnection;


    MyService myService;
    private int progress = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ButterKnife.bind(this);

        serviceConnection=new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                myService=((MyService.MyBinder) service).getService();
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {

            }
        };

        final Intent intent=new Intent(MainActivity.this,MyService.class);

        bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                myService.startDownLoad();

                listenProgress();
            }
        });

        MyThread thread=new MyThread();

    }

    public void listenProgress(){
        new Thread(new Runnable() {

            @Override
            public void run() {
                while(progress < myService.MAX_PROGRESS){
                    progress = myService.getProgress();
                    progressBar.setProgress(progress);
                    System.out.println("progress="+progress);
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

            }
        }).start();
    }


    public class MyThread extends Thread{
        @Override
        public void run() {
            super.run();
        }
    }

}

 

 

package com.example.jikangwang.myapplication;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.support.annotation.Nullable;

public class MyService extends Service {

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

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


    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return new MyBinder();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return super.onStartCommand(intent, flags, startId);

    }

    public void startDownLoad(){
        new Thread(new Runnable() {

            @Override
            public void run() {
                while(progress < MAX_PROGRESS){
                    progress += 5;
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                }
            }
        }).start();
    }


    public class MyBinder extends Binder {
        public MyService getService(){
            return MyService.this;
        }
    }
}

 

posted @ 2018-05-22 20:35  miao_a_miao  阅读(386)  评论(0编辑  收藏  举报