zoukankan      html  css  js  c++  java
  • 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;
            }
        }
    }
  • 相关阅读:
    辨异 —— 冠词(定冠词、不定冠词、零冠词)
    辨异 —— 冠词(定冠词、不定冠词、零冠词)
    dot 语法全介绍
    dot 语法全介绍
    图像的简单认识
    图像的简单认识
    向量点乘(内积)和叉乘(外积、向量积)概念及几何意义解读
    图的重要性质
    Android下载文件提示文件不存在。。。 java.io.FileNotFoundException
    Java程序猿的JavaScript学习笔记(5——prototype和Object内置方法)
  • 原文地址:https://www.cnblogs.com/cnblogs321114287/p/9073936.html
Copyright © 2011-2022 走看看