• 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;
            }
        }
    }
  • 相关阅读:
    数组方法总结
    CSS3总结
    关于h5的一些知识整理
    如何去掉iview里面的input,button等一系列标签自带的蓝色边框
    CSS隐藏多余的文字
    百度搜索之历史搜索记录~
    transform相关~
    有关数组的相关知识~~
    [Javascript]js中所学知识点回顾总结
    js_随即生成多位验证码及变换颜色
  • 原文地址:https://www.cnblogs.com/cnblogs321114287/p/9073936.html
Copyright © 2020-2023  润新知