android Activity绑定Service
activity可以绑定Service,并且可以调用Service中定义的方法
Service代码:在里面多了一个IBinder;个人理解是用来与Activity绑定的主要通道;
public class MainServer extends Service {
private final String TAG = "Service------->";
private final IBinder binder = new MyBinder(); //绑定器
public class MyBinder extends Binder {
public MainServer getService() {
return MainServer.this; //返回本服务
}
}
/** 绑定时执行 */
@Override
public IBinder onBind(Intent intent) {
Log.i(TAG, "onBind()");
return binder;
}
/** 只在创建时执行一次 */
@Override
public void onCreate() {
super.onCreate();
Log.i(TAG, "onCreate()");
}
/** 断开绑定或者停止服务时执行 */
@Override
public void onDestroy() {
super.onDestroy();
Log.i(TAG, "onDestroy()");
}
/** 当内存不够是执行该方法 */
@Override
public void onLowMemory() {
super.onLowMemory();
Log.i(TAG, "onLowMemory()");
onDestroy();
}
/** 当从新绑定是执行 */
@Override
public void onRebind(Intent intent) {
// TODO Auto-generated method stub
super.onRebind(intent);
Log.i(TAG, "onRebind()");
}
@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);
Log.i(TAG, "onStart()");
}
/** 每次执行Service都会执行该方法 */
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(TAG, "onStartCommand --->flags = " + flags + " startId = " + startId);
con = new Connect();
con.start();
return super.onStartCommand(intent, flags, startId);
}
/** 断开绑定是执行 */
@Override
public boolean onUnbind(Intent intent) {
Log.i(TAG, "onUnbind()");
return super.onUnbind(intent);
}
MainActivity代码: 必须声明实例ServiceConnection,并且在绑定成功后获取到Service,用以调用Service的方法
public class MainActivity extends Activity {
private MainServer mainService;
//绑定Service监听
ServiceConnection sconnection = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
Log.i("activity---->", "已断开Service");
}
/**当绑定时执行*/
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
Log.i("activity---->", "已绑定到Service");
mainService = ((MainServer.MyBinder)service).getService();
Intent i = new Intent();
mainService.onStartCommand(i, 0, 0); //绑定成功后可以调用Service方法,此处等与是启动Service
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button start = (Button)findViewById(R.id.start);
Button stop = (Button)findViewById(R.id.stop);
Button send = (Button)findViewById(R.id.send);
Button skip = (Button)findViewById(R.id.skip);
start.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent bind = new Intent(MainActivity.this,MainServer.class);
bindService(bind, sconnection, Context.BIND_AUTO_CREATE);
}
});
stop.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
unbindService(sconnection);
}
});
android Activity绑定Service的更多相关文章
- Tabhost中Activity绑定Service
在android中,一个Activity绑定一个Service组件我们一般用Context().bindService方法就能够.可是假设这个 Activity属于一个Tabhost的话就不行了,在网 ...
- Android Activity与Service的交互方式
参考: http://blog.csdn.net/gebitan505/article/details/18151203 实现更新下载进度的功能 1. 通过广播交互 Server端将目前的下载进度,通 ...
- Android Activity、Service、BroadcastReceiver 的生命周期
Activity.Service.BroadcastReceiver这三个组建是Android开发中最常使用到的组件,在它们的生命周期的各个阶段我们需要针对性的做些事情,了解这些组件的生命周期有利于我 ...
- Android——Activity and Service
参考博客:http://www.cnblogs.com/newcj/archive/2011/05/30/2061370.html http://www.cnblogs.com/feisky/ar ...
- android学习-IPC机制之ACtivity绑定Service通信
bindService获得Service的binder对象对服务进行操作 Binder通信过程类似于TCP/IP服务连接过程binder四大架构Server(服务器),Client(客户端),Serv ...
- Android activity和service的生命周期对比
1Activity生命周期 七个方法 1. void onCreate(Bundle savedInstanceState) 当Activity被第首次加载时执行.我们新启动一个程序的时候其主窗体的o ...
- Activity与Service通信(不同进程之间)
使用Messenger 上面的方法只能在同一个进程里才能用,如果要与另外一个进程的Service进行通信,则可以用Messenger. 其实实现IPC(Inter-Process Communicat ...
- Activity与Service通信的方式有三种:
在博客园看到的,看着挺不错的,借来分享下 继承Binder类 这个方式仅仅有当你的Acitivity和Service处于同一个Application和进程时,才干够用,比方你后台有一个播放背景音乐的S ...
- Activity与Service通信
Activity与Service通信的方式有三种: 继承Binder类 这个方式只有当你的Acitivity和Service处于同一个Application和进程时,才可以用,比如你后台有一个播放背景 ...
随机推荐
- redis中setbit的用法
原文地址:http://www.zhihu.com/question/27672245 在redis中,存储的字符串都是以二级制的进行存在的.举例:设置一个 key-value ,键的名字叫“andy ...
- oracle修改字符集
在linux下面安装了oracle 11G,由于安装的时候没有注意导致字符集问题,impdp导入操作后中文乱码. 1.用命令select userenv('language') from dual; ...
- Android工作学习第5天之TabHost实现菜单栏底部显示
TabHost是一个装载选项卡窗口的容器,实现分模块显示的效果.像新浪微博客户端.微信客户端都是使用tabehost组件来开发的. TabHost的组成: |---TabWidget:实现标签栏,可供 ...
- javaScript 相关笔记
1.js中对象复制 思路:将js对象先转成json字符串,然后再将json字符串转换为两个对象
- Iphone连接win10电脑正常, itunes无法识别解决
1.之前试过重装itunes, apple的两个服务也都已经启动. 2.但是设备管理器不显示移动设备或者usb的apple mobile device driver 3.解决:打开C:\Program ...
- What is SPI?
原文地址:http://www.fpga4fun.com/SPI1.html SPI is a simple interface that allows one chip to communicate ...
- java笔试面试二
http://www.cnblogs.com/lanxuezaipiao/p/3371224.html
- Linux下随机密码生成器
参考资料: 1:http://justwinit.cn/post/5164/ 2:http://www.linuxidc.com/Linux/2012-11/73687.htm
- 第六百一十一天 how can I 坚持
离开泛华了,在那感觉确实挺压抑的,什么环境才适合我呢.哎. 明天回济南,弟弟交房了,去看看房子,和郭娜跨个年,好好谈吧,尽快结婚了. 睡觉.
- JAVAC 命令详解(转)
本文来自:http://www.cnblogs.com/JeffChen/archive/2008/01/16/1041783.html 结构 javac [ options ] [ sourcefi ...