Serivce通知Activity更新UI方法总结

来源:互联网 发布:淘宝潮店推荐 编辑:程序博客网 时间:2024/05/12 11:52

转载自 http://blog.163.com/ppy2790@126/blog/static/1032422412013112542544572/

Activity与Service是Android的两个重要组件,在使用过程中我们遇到最多的是他们之间通讯的问题。


1、Activity向Service传递消息的方法:
    1)利用BroadcastReceiver发送广播,Activity发送广播,Service中定义广播接收者进行接收。
    2)利用绑定服务的方式开启服务,暴露服务中的方法,Activity进行调用。
    3)利用Intent打开服务(开启服务)的方式,通过Intent传递数据。

2、Service向Activity传递消息的方法:
    1)利用BroadcastReceiver,在Service中发送广播,Activity中接收。
    2)利用Handler在Service中发送消息,Activity中handleMessage进行处理。

下面以音乐播放器案例来,对以上的方式进行示例说明。
在这个案例中,Activity界面按钮(播放、暂停、停止)要发送消息给Service(负责音乐播放),Service要把播放进度通知给Activity进行播放进度的更新。

程序的界面很简单:
Serivce通知Activity更新UI方法总结 - ppy2790@126 - ITAIR
 
 
一、BroadcastReceiver实现Activity与Service相互通讯:
Activity中:

//点击播放、暂停按钮

public void play(View view) {

Intent intent = new Intent();

intent.putExtra("ctrl", 1);

intent.setAction("org.itair.service.receiver");

sendBroadcast(intent);

}


//点击停止按钮

public void stop(View view) {

Intent intent = new Intent();

intent.putExtra("ctrl", 3);

intent.setAction("org.itair.service.receiver");

sendBroadcast(intent);

}



------------------------------------------------------------------------


Service中动态注册一个广播接收者:

ServiceRecevier serviceRecevier = new ServiceRecevier();

IntentFilter intentFilter = new IntentFilter();

intentFilter.addAction("org.itair.service.receiver");

registerReceiver(serviceRecevier, intentFilter);


Service中的BroadcastReceiver,收到后,同时也向Activity发送广播,(同样在Activity中也需要注册广播接收者)

public class ServiceRecevier extends BroadcastReceiver {


@Override

public void onReceive(Context context, Intent intent) {

// 接收到Activity播放消息类型后,进行判断,调用服务中播放,暂停,停止的方法

int ctrl = intent.getExtras().getInt("ctrl", -1);

int type = -1;

switch (ctrl) {

case 1:

type = 1;

if (!isPlay) {

prepareAndStart(0, current);

else {

pause();

}

break;

case 2:

pause();

break;

case 3:

stop();

type = 0;

break;

}


//接收消息后,同时向Activit发送在播放状态,播放曲目id给Activity

//Activity收到消息后,会播放按钮更新为暂停状态

Intent sendIntent = new Intent();

sendIntent.putExtra("type", type);

sendIntent.putExtra("status"isPlay);

sendIntent.setAction("org.itair.ui.receiver");

sendBroadcast(sendIntent);


}


}


Service中音乐播放时把播放进度用广播发送给Activity:

       //Service中播放音乐的方法

public void prepareAndStart(int index, int current) {


try {

AssetFileDescriptor afd = am.openFd(musics[index]);

mPlayer.reset();

mPlayer.setDataSource(afd.getFileDescriptor(),

afd.getStartOffset(), afd.getLength());


mPlayer.prepare();

mPlayer.seekTo(current);

mPlayer.start();


isPlay = true;


final int total = mPlayer.getDuration();

Runnable runnable = new Runnable() {

Intent sendIntent = new Intent();


@Override

public void run() {

// TODO Auto-generated method stub


while (isPlay) {

// 发送进度广播给Activity

sendIntent.putExtra("type", 2);

sendIntent.putExtra("current",

100 *mPlayer.getCurrentPosition() / total);

sendIntent.setAction("org.itair.ui.receiver");

sendBroadcast(sendIntent);

try {

Thread.sleep(1000);

catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}


}


}

};


new Thread(runnable).start();


mPlayer.setOnCompletionListener(new OnCompletionListener() {


@Override

public void onCompletion(MediaPlayer mp) {

// TODO Auto-generated method stub

//播放完毕发送消息给Activity

isPlay = false;

Intent sendIntent = new Intent();

sendIntent.putExtra("type", 0);

sendIntent.putExtra("status"isPlay);

sendIntent.setAction("org.itair.ui.receiver");

sendBroadcast(sendIntent);


}

});


catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}


}


----------------------------------------------------------------------


 相应的,Activity中的广播接收者,接收广播进行处理:

public class UiRecevier extends BroadcastReceiver {


@Override

public void onReceive(Context context, Intent intent) {

int type = intent.getIntExtra("type", -1);

switch (type) {

//更新播放按钮

case 0:

pb.setProgress(0);

btn_start.setImageResource(R.drawable.play);

break;

case 1:

boolean isPlay = intent.getExtras().getBoolean("status");

tv_singer.setText(titles[0]);


if (isPlay) {

btn_start.setImageResource(R.drawable.pause);

else {

btn_start.setImageResource(R.drawable.play);

}

break;


case 2:

//更新进度

pb.setProgress(intent.getExtras().getInt("current"));

break;

}

}

}


二、采用绑定服务开启服务的方法暴露Service中的方法,Activity进行调用,Service利用Handler向Activity发送进度消息。(绑定服务+Handler进行通讯)


Activity中绑定服务:

conn = new MyConn();

Intent intent = new Intent(this, MusicService.class);

bindService(intent, connBIND_AUTO_CREATE);


Service中向Activity中发送播放进度消息(Handler+Message)


Runnable runnable = new Runnable() {

Intent sendIntent = new Intent();


Message message = null;

@Override

public void run() {

// TODO Auto-generated method stub

while (isPlay) {

//Handler message实现向Activity发送播放进度消息

message = new Message();

message.arg1 = 100 *mPlayer.getCurrentPosition()

/ total;

MainActivity.handler.sendMessage(message);

try {

Thread.sleep(1000);

catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}


}


}

};


new Thread(runnable).start();


----------------------------------------------------------------------

注意这个handler是在Activity中声明定义的,且要声明为public static。

Activity中,handler接收到消息后进行进度条的更新。


                public static Handler handler;


handler = new Handler() {


@Override

public void handleMessage(Message msg) {

// TODO Auto-generated method stub

pb.setProgress(msg.arg1);

if(msg.arg2==100){

pb.setProgress(0);

btn_start.setImageResource(R.drawable.play);

isPlay = false;

}

                                super.handleMessage(msg);

}

};


三、其它方式:
      Activity向Service发送消息,还可以采用Intent,Service采用开启服务(startService)的方式。
      Service向Activity发送消息,网上也有说可以采用Intent打开Activity的方法,但用在播放器更新进度时,不太合适。

0 0
原创粉丝点击