Serivce通知Activity更新UI方法总结
来源:互联网 发布:淘宝潮店推荐 编辑:程序博客网 时间:2024/05/12 11:52
转载自 http://blog.163.com/ppy2790@126/blog/static/1032422412013112542544572/
Activity与Service是Android的两个重要组件,在使用过程中我们遇到最多的是他们之间通讯的问题。
//点击播放、暂停按钮
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, conn, BIND_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);
}
};
- Serivce通知Activity更新UI方法总结
- Serivce通知Activity更新UI方法总结
- Serivce通知Activity更新UI方法总结
- Android Service 通知Activity更新界面的方法研究|Service通过Broadcast更新UI
- Android Service 通知Activity更新界面的方法研究|Service通过Broadcast更新UI
- Android Service 通知Activity更新界面的方法研究|Service通过Broadcast更新UI
- Android Service 通知Activity更新界面的方法研究
- Android Service 通知Activity更新界面的方法研究
- Android Service 通知Activity更新界面的方法研究
- Android Service 通知Activity更新界面的方法研究
- Android Service 通知Activity更新界面的方法
- Android Service 通知Activity更新界面的方法
- Android Service 通知Activity更新界面的方法研究
- service通知activity修改ui
- Service通知Activity修改UI
- 总结五种更新UI的方法
- 安卓开发观察者模式应用-一个service通知多个activity更新ui的实现
- activity 使用runOnUiThread更新 ui
- POJ 2337 Catenyms 欧拉回路
- Numeber类——包装类
- 模块加载过程代码分析1
- 网易秋招编程题——最大的奇约数
- hibernate中的二级缓存的问题
- Serivce通知Activity更新UI方法总结
- js验证url的格式合法性
- UML-类图
- 小功能--留言板
- BZOJ1878: [SDOI2009]HH的项链(树状数组+离线)
- Hololens开发环境配置
- Android 努比亚Z9min root 以及 data文件夹读写权限开放
- 【HDOJ】5907 Find Q
- 树的三种非递归java实现