绑定服务:调用服务内部的方法
来源:互联网 发布:对象转成json 编辑:程序博客网 时间:2024/04/30 19:59
service:后台运行 没有界面的组件
Intent
startService();
stopService();
服务一旦被创建,就不会重新创建了.
服务的oncreate方法只会执行一次,如果服务已经开启,就不会再去执行oncreate方法
服务在停止的时候,会调用ondestroy方法
每一次开启服务的时候 都会调用 onStartCommand() onStart();
服务有两种开启方式:
1. startService(); 服务一旦开启就跟开启者(activity)没有什么关系.activity挂了,
服务还会开心的在后台运行.
2. bindService(); 服务跟activity就有了千丝万缕的联系. 如果调用者activity挂了
服务也跟着挂了. 不求同时生,但求同时死.
服务只能被显示的解除绑定一次,多次的解除绑定服务 应用程序会报异常
----------------------
绑定服务调用服务里面方法的流程:
1. 在activity 采用bindService方式开启服务...
2. 写一个实现类 MyConn implements ServiceConnection
3. 接口里面有一个方法onServiceConnected 在服务成功绑定的时候调用的方法
4. 在service代码 实现 onBind方法 返回一个IBinder接口的实现( 里面必须一个调用服务方法的api)
5. 在服务成功绑定的时候 服务里面返回的IBinder对象会传给 activity里面onServiceConnected 方法
6. 获取IBinder对象 中间人, 调用中间人的方法. 间接调用了服务里面的方法
绑定服务调用服务里面方法的流程:
1. 在activity 采用bindService方式开启服务...
2. 写一个实现类 MyConn implements ServiceConnection
3. 接口里面有一个方法onServiceConnected 在服务成功绑定的时候调用的方法
4. 在service代码 实现 onBind方法 返回一个IBinder接口的实现( 里面必须一个调用服务方法的api)
5. 在服务成功绑定的时候 服务里面返回的IBinder对象会传给 activity里面onServiceConnected 方法
6. 获取IBinder对象 中间人, 调用中间人的方法. 间接调用了服务里面的方法
-------------------------------------------------------------------------------------------
public class MainActivity extends Activity {
private ServiceConnection conn;
private IService mybinder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout. activity_main);
}
/**
* 开启服务:已绑定服务的方式获取服务中的代理人,间接调用服务内的方法
*/
public void start(View view){
System. out.println( "服务开启...");
Intent intent= new Intent(getApplicationContext(),MyService.class );
conn= new MyConn();
bindService(intent, conn, BIND_AUTO_CREATE);
// onBindService(conn); conn=null; 关闭服务并将服务置为空
}
// 2.实现serviceConnection接口
private class MyConn implements ServiceConnection{
//服务成功绑定时
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mybinder= (IService) service; //获取代理人对象
System. out.println( "Activity得到服务返回的代理人....." );
}
//当服务异常终止....失去连接的时候调用的方法
@Override
public void onServiceDisconnected(ComponentName name) {
}
}
/**
* 调用服务内方法
* @param view
*/
public void getMethod(View view){
mybinder.Method(15000);
}
}
-------------------------------------------------------------------------------------------
/**
* 成功绑定服务时调用的方法
*/
@Override
public IBinder onBind(Intent arg0) {
System. out.println( "服务被成功绑定时....返回代理人对象" );
return new MyBind();
}
/**
* 将服务分方法交给代理人代理
* @author Administrator
*
*/
public class MyBind extends Binder{
/**
* 服务内的方法
* @param money
*/
public boolean Method( long money){
if(money>10000){
System. out.println( "卖给你清华大学毕业证了...." );
Toast. makeText(getApplicationContext(), "卖给你清华大学毕业证了...." , 0).show();
return true;
} else{
System. out.println( "没钱还想买证书?......" );
Toast. makeText(getApplicationContext(), "没钱还想买证书?......", 0).show();
return false;
}
}
}
}
--------------------------------------------------------------------------------------
public interface IService {
public boolean Method( long money);
}
- 绑定服务:调用服务内部的方法
- 绑定服务&调用服务里面的方法
- andorid,绑定服务,调用服务的方法
- Android--绑定服务调用服务的方法
- 绑定服务并调用服务的方法,解除绑定
- 绑定的方式调用服务的方法
- 绑定服务调用方法的回顾
- android,aidl,绑定远程服务,调用远程服务的方法
- android之绑定服务调用服务的方法
- Android绑定方式开始服务&调用服务当中的方法
- Android启动服务和绑定服务的方法调用
- 绑定服务:调用远程服务中的方法
- 绑定服务并调用服务中的方法
- 绑定服务调用本地服务中的方法
- 绑定服务调用远程服务中的方法
- activity以绑定服务的方式开启服务并调用服务里面的方法
- android 服务的绑定调用
- 绑定和解除绑定远程服务并调用远程服务的方法
- 开机启动项:电话窃听器
- Linux下查找文件
- Flash字体嵌入方法
- HDU3530
- 全局最小割
- 绑定服务:调用服务内部的方法
- opencv在Linux下的安装及其测试
- a^b%c问题 ---模板
- 大数阶乘Java实现
- ACM题目:第一行输入n m,第二行输入一个数列,n为数列长度,m为要插入的值,排序后输出,m n为零时退出程序
- 在VS2008中使用jQuery智能感应的方法
- 未来期望与目标
- ios Socket套接字编程
- POJ 1286 Necklace of Beads