您的位置:首页 > 移动开发 > Android开发

Android Service与Activity之间通信:通过Binder对象、Broadcast广播

2016-01-28 14:28 561 查看


Android Service与Activity之间通信:通过Binder对象、Broadcast广播

2014年09月05日 ⁄ 综合 ⁄ 共 15251字 ⁄ 字号 小 中 大 ⁄ 评论关闭

From:http://blog.csdn.net/xiaanming/article/details/9750689

From:http://blog.csdn.net/ameyume/article/details/6290137

From:http://blog.csdn.net/xiaanming/article/details/9750689

From:http://blog.csdn.net/ameyume/article/details/6290137

在Android中,Activity主要负责前台页面的展示,Service主要负责需要长期运行的任务,所以在我们实际开发中,就会常常遇到Activity与Service之间的通信,我们一般在Activity中启动后台Service,通过Intent来启动,Intent中我们可以传递数据给Service,而当我们Service执行某些操作之后想要更新UI线程,我们应该怎么做呢?接下来我就介绍两种方式来实现Service与Activity之间的通信问题

通过Binder对象

当Activity通过调用bindService(Intent service, ServiceConnection conn,int flags),我们可以得到一个Service的一个对象实例,然后我们就可以访问Service中的方法,我们还是通过一个例子来理解一下吧,一个模拟下载的小例子,带大家理解一下通过Binder通信的方式

首先我们新建一个工程Communication,然后新建一个Service类

[java] view plaincopy

<span style="font-family:System;">package com.example.communication;  

  

import android.app.Service;  

import android.content.Intent;  

import android.os.Binder;  

import android.os.IBinder;  

  

public class MsgService extends Service {  

    /** 

     * 进度条的最大值 

     */  

    public static final int MAX_PROGRESS = 100;  

    /** 

     * 进度条的进度值 

     */  

    private int progress = 0;  

  

    /** 

     * 增加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;  

                    try {  

                        Thread.sleep(1000);  

                    } catch (InterruptedException e) {  

                        e.printStackTrace();  

                    }  

               
4000
      

                }  

            }  

        }).start();  

    }  

  

  

    /** 

     * 返回一个Binder对象 

     */  

    @Override  

    public IBinder onBind(Intent intent) {  

        return new MsgBinder();  

    }  

      

    public class MsgBinder extends Binder{  

        /** 

         * 获取当前Service的实例 

         * @return 

         */  

        public MsgService getService(){  

            return MsgService.this;  

        }  

    }  

  

}</span>  

上面的代码比较简单,注释也比较详细,最基本的Service的应用了,相信你看得懂的,我们调用startDownLoad()方法来模拟下载任务,然后每秒更新一次进度,但这是在后台进行中,我们是看不到的,所以有时候我们需要他能在前台显示下载的进度问题,所以我们接下来就用到Activity了

[java] view plaincopy

Intent intent = new Intent("com.example.communication.MSG_ACTION");    

bindService(intent, conn, Context.BIND_AUTO_CREATE);  

通过上面的代码我们就在Activity绑定了一个Service,上面需要一个ServiceConnection对象,它是一个接口,我们这里使用了匿名内部类

[java] view plaincopy

<span style="font-family:System;">  ServiceConnection conn = new ServiceConnection() {  

          

        @Override  

        public void onServiceDisconnected(ComponentName name) {  

              

        }  

          

        @Override  

        public void onServiceConnected(ComponentName name, IBinder service) {  

            //返回一个MsgService对象  

            msgService = ((MsgService.MsgBinder)service).getService();  

              

        }  

    };</span>  

在onServiceConnected(ComponentName name, IBinder service) 回调方法中,返回了一个MsgService中的Binder对象,我们可以通过getService()方法来得到一个MsgService对象,然后可以调用MsgService中的一些方法,Activity的代码如下

[java] view plaincopy

<span style="font-family:System;">package com.example.communication;  

  

import android.app.Activity;  

import android.content.ComponentName;  

import android.content.Context;  

import android.content.Intent;  

import android.content.ServiceConnection;  

import android.os.Bundle;  

import android.os.IBinder;  

import android.view.View;  

import android.view.View.OnClickListener;  

import android.widget.Button;  

import android.widget.ProgressBar;  

  

public class MainActivity extends Activity {  

    private MsgService msgService;  

    private int progress = 0;  

    private ProgressBar mProgressBar;  

      

  

    @Override  

    protected void onCreate(Bundle savedInstanceState) {  

        super.onCreate(savedInstanceState);  

        setContentView(R.layout.activity_main);  

          

          

        //绑定Service  

        Intent intent = new Intent("com.example.communication.MSG_ACTION");  

        bindService(intent, conn, Context.BIND_AUTO_CREATE);  

          

          

        mProgressBar = (ProgressBar) findViewById(R.id.progressBar1);  

        Button mButton = (Button) findViewById(R.id.button1);  

        mButton.setOnClickListener(new OnClickListener() {  

              

            @Override  

            public void onClick(View v) {  

                //开始下载  

                msgService.startDownLoad();  

                //监听进度  

                listenProgress();  

            }  

        });  

          

    }  

      

  

    /** 

     * 监听进度,每秒钟获取调用MsgService的getProgress()方法来获取进度,更新UI 

     */  

    public void listenProgress(){  

        new Thread(new Runnable() {  

              

            @Override  

            public void run() {  

                while(progress < MsgService.MAX_PROGRESS){  

                    progress = msgService.getProgress();  

                    mProgressBar.setProgress(progress);  

                    try {  

                        Thread.sleep(1000);  

                    } catch (InterruptedException e) {  

                        e.printStackTrace();  

                    }  

                }  

                  

            }  

        }).start();  

    }  

      

    ServiceConnection conn = new ServiceConnection() {  

        @Override  

        public void onServiceDisconnected(ComponentName name) {  

              

        }  

          

        @Override  

        public void onServiceConnected(ComponentName name, IBinder service) {  

            //返回一个MsgService对象  

            msgService = ((MsgService.MsgBinder)service).getService();  

              

        }  

    };  

  

    @Override  

    protected void onDestroy() {  

        unbindService(conn);  

        super.onDestroy();  

    }  

  

  

}</span><span style="font-family: simsun;">  

</span>  

其实上面的代码我还是有点疑问,就是监听进度变化的那个方法我是直接在线程中更新UI的,不是说不能在其他线程更新UI操作吗,可能是ProgressBar比较特殊吧,我也没去研究它的源码,知道的朋友可以告诉我一声,谢谢!

上面的代码就完成了在Service更新UI的操作,可是你发现了没有,我们每次都要主动调用getProgress()来获取进度值,然后隔一秒在调用一次getProgress()方法,你会不会觉得很被动呢?可不可以有一种方法当Service中进度发生变化主动通知Activity,答案是肯定的,我们可以利用回调接口实现Service的主动通知,不理解回调方法的可以看看http://blog.csdn.net/xiaanming/article/details/8703708

新建一个回调接口

[java] view plaincopy

public interface OnProgressListener {  

    void onProgress(int progress);  

}  

MsgService的代码有一些小小的改变,为了方便大家看懂,我还是将所有代码贴出来

[java] view plaincopy

<span style="font-family:System;">package com.example.communication;  

  

import android.app.Service;  

import android.content.Intent;  

import android.os.Binder;  

import android.os.IBinder;  

  

public class MsgService extends Service {  

    /** 

     * 进度条的最大值 

     */  

    public static final int MAX_PROGRESS = 100;  

    /** 

     * 进度条的进度值 

     */  

    private int progress = 0;  

      

    /** 

     * 更新进度的回调接口 

     */  

    private OnProgressListener onProgressListener;  

      

      

    /** 

     * 注册回调接口的方法,供外部调用 

     * @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){  

                        onProgressListener.onProgress(progress);  

                    }  

                      

                    try {  

                        Thread.sleep(1000);  

                    } catch (InterruptedException e) {  

                        e.printStackTrace();  

                    }  

                      

                }  

            }  

        }).start();  

    }  

  

  

    /** 

     * 返回一个Binder对象 

     */  

    @Override  

    public IBinder onBind(Intent intent) {  

        return new MsgBinder();  

    }  

      

    public class MsgBinder extends Binder{  

        /** 

         * 获取当前Service的实例 

         * @return 

         */  

        public MsgService getService(){  

            return MsgService.this;  

        }  

    }  

  

}</span>  

Activity中的代码如下

[java] view plaincopy

<span style="font-family:System;">package com.example.communication;  

  

import android.app.Activity;  

import android.content.ComponentName;  

import android.content.Context;  

import android.content.Intent;  

import android.content.ServiceConnection;  

import android.os.Bundle;  

import android.os.IBinder;  

import android.view.View;  

import android.view.View.OnClickListener;  

import android.widget.Button;  

import android.widget.ProgressBar;  

  

public class MainActivity extends Activity {  

    private MsgService msgService;  

    private ProgressBar mProgressBar;  

      

  

    @Override  

    protected void onCreate(Bundle savedInstanceState) {  

        super.onCreate(savedInstanceState);  

        setContentView(R.layout.activity_main);  

          

          

        //绑定Service  

        Intent intent = new Intent("com.example.communication.MSG_ACTION");  

        bindService(intent, conn, Context.BIND_AUTO_CREATE);  

          

          

        mProgressBar = (ProgressBar) findViewById(R.id.progressBar1);  

        Button mButton = (Button) findViewById(R.id.button1);  

        mButton.setOnClickListener(new OnClickListener() {  

              

            @Override  

            public void onClick(View v) {  

                //开始下载  

                msgService.startDownLoad();  

            }  

        });  

          

    }  

      

  

    ServiceConnection conn = new ServiceConnection() {  

        @Override  

        public void onServiceDisconnected(ComponentName name) {  

              

        }  

          

        @Override  

        public void onServiceConnected(ComponentName name, IBinder service) {  

            //返回一个MsgService对象  

            msgService = ((MsgService.MsgBinder)service).getService();  

              

            //注册回调接口来接收下载进度的变化  

            msgService.setOnProgressListener(new OnProgressListener() {  

                  

                @Override  

                public void onProgress(int progress) {  

                    mProgressBar.setProgress(progress);  

                      

                }  

            });  

              

        }  

    };  

  

    @Override  

    protected void onDestroy() {  

        unbindService(conn);  

        super.onDestroy();  

    }  

  

  

}  

</span>  

用回调接口是不是更加的方便呢,当进度发生变化的时候Service主动通知Activity,Activity就可以更新UI操作了

通过broadcast(广播)的形式

当我们的进度发生变化的时候我们发送一条广播,然后在Activity的注册广播接收器,接收到广播之后更新ProgressBar,代码如下

[java] view plaincopy

package com.example.communication;  

<span style="font-family:System;">  

import android.app.Activity;  

import android.content.BroadcastReceiver;  

import android.content.Context;  

import android.content.Intent;  

import android.content.IntentFilter;  

import android.os.Bundle;  

import android.view.View;  

import android.view.View.OnClickListener;  

import android.widget.Button;  

import android.widget.ProgressBar;  

  

public class MainActivity extends Activity {  

    private ProgressBar mProgressBar;  

    private Intent mIntent;  

    private MsgReceiver msgReceiver;  

      

  

    @Override  

    protected void onCreate(Bundle savedInstanceState) {  

        super.onCreate(savedInstanceState);  

        setContentView(R.layout.activity_main);  

          

        //动态注册广播接收器  

        msgReceiver = new MsgReceiver();  

        IntentFilter intentFilter = new IntentFilter();  

        intentFilter.addAction("com.example.communication.RECEIVER");  

        registerReceiver(msgReceiver, intentFilter);  

          

          

        mProgressBar = (ProgressBar) findViewById(R.id.progressBar1);  

        Button mButton = (Button) findViewById(R.id.button1);  

        mButton.setOnClickListener(new OnClickListener() {  

              

            @Override  

            public void onClick(View v) {  

                //启动服务  

                mIntent = new Intent("com.exa
b1fe
mple.communication.MSG_ACTION");  

                startService(mIntent);  

            }  

        });  

          

    }  

  

      

    @Override  

    protected void onDestroy() {  

        //停止服务  

        stopService(mIntent);  

        //注销广播  

        unregisterReceiver(msgReceiver);  

        super.onDestroy();  

    }  

  

  

    /** 

     * 广播接收器 

     * @author len 

     * 

     */  

    public class MsgReceiver extends BroadcastReceiver{  

  

        @Override  

        public void onReceive(Context context, Intent intent) {  

            //拿到进度,更新UI  

            int progress = intent.getIntExtra("progress", 0);  

            mProgressBar.setProgress(progress);  

        }  

          

    }  

  

}  

</span>  

[java] view plaincopy

<span style="font-family:System;">package com.example.communication;  

  

import android.app.Service;  

import android.content.Intent;  

import android.os.IBinder;  

  

public class MsgService extends Service {  

    /** 

     * 进度条的最大值 

     */  

    public static final int MAX_PROGRESS = 100;  

    /** 

     * 进度条的进度值 

     */  

    private int progress = 0;  

      

    private Intent intent = new Intent("com.example.communication.RECEIVER");  

      

  

    /** 

     * 模拟下载任务,每秒钟更新一次 

     */  

    public void startDownLoad(){  

        new Thread(new Runnable() {  

              

            @Override  

            public void run() {  

                while(progress < MAX_PROGRESS){  

                    progress += 5;  

                      

                    //发送Action为com.example.communication.RECEIVER的广播  

                    intent.putExtra("progress", progress);  

                    sendBroadcast(intent);  

                      

                    try {  

                        Thread.sleep(1000);  

                    } catch (InterruptedException e) {  

                        e.printStackTrace();  

                    }  

                      

                }  

            }  

        }).start();  

    }  

  

      

  

    @Override  

    public int onStartCommand(Intent intent, int flags, int startId) {  

        startDownLoad();  

        return super.onStartCommand(intent, flags, startId);  

    }  

  

  

  

    @Override  

    public IBinder onBind(Intent intent) {  

        return null;  

    }  

  

  

}</span>  

总结:

Activity调用bindService (Intent service, ServiceConnection conn, int flags)方法,得到Service对象的一个引用,这样Activity可以直接调用到Service中的方法,如果要主动通知Activity,我们可以利用回调方法

 Service向Activity发送消息,可以使用广播,当然Activity要注册相应的接收器。比如Service要向多个Activity发送同样的消息的话,用这种方法就更好

 Android中service的作用相信大家都很清楚了,主要是在后台执行操作,没有画面,类似于windows中的服务(service); 并且可以在前台activity画面退出时,继续执行后台的服务。

      启动service的方法有两种,一种是startService,一种是bindService,都是通过Intent作为媒介来启动service的。如果用户是用startService方式启动的服务,想从后台service发送数据给前台,执行画面显示或者更新,那该如何实现呢?这是就可以使用android系统的broadcast组件,通过广播的形式在servcie中发送广播,通过启动广播时的Intent传送需要更新的数据,当前台activity接收到这个广播后,就可以执行数据显示或者更新画面操作了。

例如,有个音乐播放程序,前台Activity是MusicDemo类,后台播放音乐的类是MusicService类。在播放时,需要在view中显示当前正在播放歌曲的曲名,专辑,演唱者,歌曲时长等信息,此时就可以在MusicService中发送广播给MusciDemo类,当前台Activity接收到数据时,显示歌曲信息。

1.在MusicService.java中:

通过sendBroadcast()发送广播,并且把数据放在intent中

其中字符串Common.NUM_COUNT_RECEIVER的定义为:

public static final String NUM_COUNT_RECEIVER = "com.min.musicdemo.action.NUM_COUNT";

 

[java] view plaincopy

// send information to update view  

Intent intent1 = new Intent(Common.NUM_COUNT_RECEIVER);  

intent1.putExtra("num", mPlayPosition + 1);  

intent1.putExtra("count", mCursor.getCount());  

intent1.putExtra("artist", artist);  

intent1.putExtra("album", album);  

intent1.putExtra("title", title);  

intent1.putExtra("totalSeconds", mMediaPlayer.getDuration() / 1000);  

sendBroadcast(intent1);  

2.在MusicDemo类中定义继承自BroadcastReceiver的内部类InnerReceiver,用于接收广播。

在InnerReceiver类的onReceive函数中接收action为Common.MUSIC_LIST_RECEIVER的广播,然后取出数据,更新画面。

[java] view plaincopy

public class MusicDemo extends Activity implements OnClickListener {  

...  

    // broadcast receiver  

    public static class InnerReceiver extends BroadcastReceiver {  

  

        @Override  

        public void onReceive(Context context, Intent intent) {  

if (intent.getAction().equals(Common.NUM_COUNT_RECEIVER)) {  

//              Log.d(TAG, "*********** NUM_COUNT_RECEIVER Start **********" + Common.getDate());  

                try{  

                    int num = intent.getIntExtra("num", 1);  

                    int count = intent.getIntExtra("count", 1);  

                    String artist = intent.getStringExtra("artist");  

                    String album = intent.getStringExtra("album");  

                    String title = intent.getStringExtra("title");  

                    int totalSecond = intent.getIntExtra("totalSeconds", 1);  

                      

                    // set music name to screen title  

                    tvTitleMusicName.setText(title);  

                    tvTitleNumCount.setText(num + "/" + count);  

                      

                    // play second  

                    tvTotalSeconds.setText(String.format("%1$02d:%2$02d",  

                            totalSecond / 60, totalSecond % 60));  

                      

                    // play information  

                    SetMusicArtist(artist, title);  

                    tvVPArtist.setText(artist);     // 艺术家  

                    tvVPAlbum.setText(album);       // 专辑  

                      

                }catch(Exception e) {  

                    Log.e(TAG, "receive NUM_COUNT error!!!!!");  

                    e.printStackTrace();  

                }  

            }  

        }  

  

    }  

3.需要在manifest文件中把MusciDemo的子类InnerReceiver注册为广播接收者(receiver),这样才能接收到action为Common.NUM_COUNT_RECEIVER的广播,代码如下:

[xhtml] view plaincopy

<receiver android:name=".MusicDemo$InnerReceiver">  

            <intent-filter>  

                 <action android:name="com.min.musicdemo.action.NUM_COUNT"/>  

            </intent-filter>  

            <intent-filter>  

                 <action android:name="com.min.musicdemo.action.MUSIC_LIST"/>  

            </intent-filter>  

        </receiver>  

 

以上就是通过广播从service传送数据到activity的一种方法,这种方法的好处是简单意义,直接使用android提供的Broadcast组件。

缺点就是使用了系统的广播体制,需要通过系统的消息队列,效率上不太高。

好的方法是通过bindService来实现activity和service之间的通信,以及AIDL方式实现远程服务之间的通信,这种方式将在下一节中介绍。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: