service
来源:互联网 发布:软件分析师 编辑:程序博客网 时间:2024/05/06 01:15
public class MainActivity extends Activity {MusicInterface mi; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Intent intent = new Intent(this, MusicService.class); //混合调用 //为了把服务所在进程变成服务进程 startService(intent); //为了拿到中间人对象 bindService(intent, new MusicServiceConn(), BIND_AUTO_CREATE); } class MusicServiceConn implements ServiceConnection{@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {// TODO Auto-generated method stubmi = (MusicInterface) service;}@Overridepublic void onServiceDisconnected(ComponentName name) {// TODO Auto-generated method stub} } //开始播放按钮 public void play(View v){ mi.play(); } //暂停播放按钮 public void pause(View v){ mi.pause(); }}
public class MusicService extends Service{@Overridepublic IBinder onBind(Intent intent) {// TODO Auto-generated method stubreturn new MusicController();}//必须继承binder,才能作为中间人对象返回class MusicController extends Binder implements MusicInterface{public void play(){MusicService.this.play();}public void pause(){MusicService.this.pause();}}public void play(){System.out.println("播放音乐");}public void pause(){System.out.println("暂停播放");}}
public interface MusicInterface {void play();void pause();}
0 0
- Service
- Service
- service
- Service
- service
- Service
- Service
- service
- Service
- Service
- Service
- service
- service
- Service
- Service
- Service
- Service
- Service
- leetcode 108 Convert Sorted Array to Binary Search Tree
- 用avalon写的一个弹窗demo
- 查看Linux系统当前是多少位
- 字符集和字符编码
- 自己写的关于实现数字阶乘的方法,一个采用普通方法,一个是采用递归
- service
- IO多路复用(select poll epoll)
- php编程——验证码的实现(session方法)
- Cookie机制理解介绍
- Ajax常见错误总结
- C++第七次作业
- Chapter 2. C++程序设计入门
- BZOJ 2124 等差子序列
- K8S实战——SIG PIPE问题造成kubernetes集群工作不正常的分析