Android前台画面和后台service之间通信的方法之Broadcast

来源:互联网 发布:网络很火的粤语歌 编辑:程序博客网 时间:2024/05/22 12:03
  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
  1. // send information to update view  
  2. Intent intent1 = new Intent(Common.NUM_COUNT_RECEIVER);  
  3. intent1.putExtra("num", mPlayPosition + 1);  
  4. intent1.putExtra("count", mCursor.getCount());  
  5. intent1.putExtra("artist", artist);  
  6. intent1.putExtra("album", album);  
  7. intent1.putExtra("title", title);  
  8. intent1.putExtra("totalSeconds", mMediaPlayer.getDuration() / 1000);  
  9. sendBroadcast(intent1);  

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

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

[java] view plaincopy
  1. public class MusicDemo extends Activity implements OnClickListener {  
  2. ...  
  3.     // broadcast receiver  
  4.     public static class InnerReceiver extends BroadcastReceiver {  
  5.   
  6.         @Override  
  7.         public void onReceive(Context context, Intent intent) {  
  8. if (intent.getAction().equals(Common.NUM_COUNT_RECEIVER)) {  
  9. //              Log.d(TAG, "*********** NUM_COUNT_RECEIVER Start **********" + Common.getDate());  
  10.                 try{  
  11.                     int num = intent.getIntExtra("num"1);  
  12.                     int count = intent.getIntExtra("count"1);  
  13.                     String artist = intent.getStringExtra("artist");  
  14.                     String album = intent.getStringExtra("album");  
  15.                     String title = intent.getStringExtra("title");  
  16.                     int totalSecond = intent.getIntExtra("totalSeconds"1);  
  17.                       
  18.                     // set music name to screen title  
  19.                     tvTitleMusicName.setText(title);  
  20.                     tvTitleNumCount.setText(num + "/" + count);  
  21.                       
  22.                     // play second  
  23.                     tvTotalSeconds.setText(String.format("%1$02d:%2$02d",  
  24.                             totalSecond / 60, totalSecond % 60));  
  25.                       
  26.                     // play information  
  27.                     SetMusicArtist(artist, title);  
  28.                     tvVPArtist.setText(artist);     // 艺术家  
  29.                     tvVPAlbum.setText(album);       // 专辑  
  30.                       
  31.                 }catch(Exception e) {  
  32.                     Log.e(TAG, "receive NUM_COUNT error!!!!!");  
  33.                     e.printStackTrace();  
  34.                 }  
  35.             }  
  36.         }  
  37.   
  38.     }  

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

[xhtml] view plaincopy
  1. <receiver android:name=".MusicDemo$InnerReceiver">  
  2.             <intent-filter>  
  3.                  <action android:name="com.min.musicdemo.action.NUM_COUNT"/>  
  4.             </intent-filter>  
  5.             <intent-filter>  
  6.                  <action android:name="com.min.musicdemo.action.MUSIC_LIST"/>  
  7.             </intent-filter>  
  8.         </receiver>  

 

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

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

好的方法是通过bindService来实现activity和service之间的通信,以及AIDL方式实现远程服务之间的通信,这种方式将在下一节中介绍。

0 0
原创粉丝点击