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和进程时,才可以用,比如你后台有一个播放背景 ...
随机推荐
- iOS收到Push后播放声音和震动
一.APNS 1.注册 [cpp] [[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNoti ...
- LeetCode 397. Integer Replacement
397. Integer Replacement QuestionEditorial Solution My Submissions Total Accepted: 5878 Total Subm ...
- Centos 6.5 SNMP客户端安装及配置版本net-snmp-5.7.3
Centos 6.5 SNMP客户端安装及配置SNMP版本:net-snmp-5.7.3.tar.gz1.下载软件cd /usr/local/srcyum -y install gccwget htt ...
- CString std::string相互转换
CString->std::string 例子: CString strMfc=“test“; std::string strStl; strStl=strMfc.GetBuffer(0); s ...
- XMLHttpRequest简单总结
一.概念 XMLHttpRequest 对象用于在后台与服务器交换数据. XMLHttpRequest 对象是能够: 在不重新加载页面的情况下更新网页 在页面已加载后从服务器请求数据 在页面已加载后从 ...
- python中logging
1.root logger以及logger斧子关系 http://www.pythonclub.org/modules/logging #coding=utf-8 __author__ = 'n ...
- iOS--KVO的实现原理与具体应用
本文分为2个部分:概念与应用. 概念部分旨在剖析KVO这一设计模式的实现原理,应用部分通过创建的项目,以说明KVO技术在iOS开发中所带来的作用: 如果是作为是刚接触KVO的初学者,可以在了解基本原理 ...
- Javaweb命名规则
一.类命名命名规范:以大写字母开头,如果有多个单词,每个单词头字母大写.例如:StudentInfo 二.接口命名命名规范:以大写字母"I"开头,如果有多个单词,每个单词头字母大写 ...
- jquery 上传回显图片预览
/******************************************************************************* * 异步上传文件,兼容IE8,火狐和谷 ...
- 使用spring通知时,代理出错
动态代理是基于接口的,spring配置是基于类的!!!!!!!!!! 注意:JDK的动态代理,只能对实现接口的类实现代理,生成代理对象,如果这个类没有实现接口,是生成不了代理对象的.如本例UserMa ...