zoukankan      html  css  js  c++  java
  • Activity与Service进行数据交互

    Android启动Service有两种方法,一种是startService,一种是bindService。生命周期如下:

    执行startService时,调用者如果没有stopService,Service会一直在后台运行。多次调用startService,该Service只能被创建一次,即该Service的onCreate方法只会被调用一次。但是每次调用startService,onStartCommand方法都会被调用。

    执行bindService时,调用者调用unbindService方法或者调用者Context不存在了(如Activity被finish了)。第一次执行bindService时,onCreate和onBind方法会被调用,但是多次执行bindService时,onCreate和onBind方法并不会被多次调用,即并不会多次创建服务和绑定服务。 多个组件可以同时绑定到该服务,但全部取消绑定后,该服务即会被销毁。

    既使用startService又使用bindService的情况,需要unbindService和stopService同时调用才会终止Service。

    Activity与Service交互有两种方法:一种是使用broadcast,另一种是使用bindService。本文只介绍bindService方法。

     1 public class MsgService extends Service {
     2     public MsgService() {
     3     }
     4 
     5     /**
     6      * 进度条的最大值
     7      */
     8     public static final int MAX_PROGRESS = 100;
     9     /**
    10      * 进度条的进度值
    11      */
    12     private int progress = 0;
    13 
    14     /**
    15      * 增加get()方法,供Activity调用
    16      *
    17      * @return 下载进度
    18      */
    19     public int getProgress() {
    20         return progress;
    21     }
    22 
    23     /**
    24      * 模拟下载任务,每秒钟更新一次
    25      */
    26     public void startDownLoad(){
    27         new Thread(new Runnable() {
    28 
    29             @Override
    30             public void run() {
    31                 while(progress < MAX_PROGRESS){
    32                     progress += 5;
    33 
    34                     //进度发生变化通知调用方
    35                     if(onProgressListener != null){
    36                         onProgressListener.onProgress(progress);
    37                     }
    38 
    39                     try {
    40                         Thread.sleep(1000);
    41                     } catch (InterruptedException e) {
    42                         e.printStackTrace();
    43                     }
    44 
    45                 }
    46             }
    47         }).start();
    48     }
    49 
    50     @Override
    51     public IBinder onBind(Intent intent) {
    52         return new MyBinder();
    53     }
    54 
    55     public class MyBinder extends Binder {
    56         public MsgService getService() {
    57             return MsgService.this;
    58         }
    59     }
    60 
    61     public interface OnProgressListener {
    62         void onProgress(int progress);
    63     }
    64 
    65     /**
    66      * 更新进度的回调接口
    67      */
    68     private OnProgressListener onProgressListener;
    69 
    70 
    71     /**
    72      * 注册回调接口的方法,供外部调用
    73      *
    74      * @param onProgressListener
    75      */
    76     public void setOnProgressListener(OnProgressListener onProgressListener) {
    77         this.onProgressListener = onProgressListener;
    78     }
    79 
    80 }
    public class MainActivity extends Activity {
    
        private Button button19;
    
        private MsgService msgService;
        private int progress = 0;
        private ProgressBar mProgressBar;
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
    
    
    
            btnExec = (Button) findViewById(R.id.btnExec);
            button19 = (Button) findViewById(R.id.button19);
            mProgressBar = (ProgressBar) findViewById(R.id.progressBar);
    
    
            button19.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    msgService.startDownLoad();
                }
            });
    
            Intent intent = new Intent(this, MsgService.class);
            bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE);
    
    
    
        }
    
    
    
    
    
        ServiceConnection mServiceConnection = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
                msgService = ((MsgService.MyBinder) iBinder).getService();
    
                msgService.setOnProgressListener(new MsgService.OnProgressListener() {
                    @Override
                    public void onProgress(int progress) {
                        mProgressBar.setProgress(progress);
                    }
                });
            }
    
            @Override
            public void onServiceDisconnected(ComponentName componentName) {
    
            }
        };
    
    
        @Override
        protected void onDestroy() {
    
            unbindService(mServiceConnection);
            super.onDestroy();
        }
    }

    例子中,MsgService模拟耗时的下载任务,MainActivity 绑定服务,通过注册OnProgressListener回调获取下载进度,更新进度条。

    本例子Activity和Service是在同一个进程中,对于跨进程调用Service需要使用到AIDL技术。

  • 相关阅读:
    table 表格的增删和修改
    js实现单双行文本溢出添加省略号
    C++
    PAT乙级 1029 旧键盘 (C++ python3)
    图论
    图论
    图论
    springcloud(二):注册中心Eureka
    apollo配置中心初探
    Apollo 配置详细步骤(Windows环境)
  • 原文地址:https://www.cnblogs.com/l2rf/p/5953214.html
Copyright © 2011-2022 走看看