bindService与startService
来源:互联网 发布:高速公路机电系统优化 编辑:程序博客网 时间:2024/06/02 04:40
1. bindService既可以启动一个跨进程的service,也可以启动一个本进程内的service。
如果没有用aidl接口,则bindService不能bind另一个进程的service
如果使用了aidl接口,则在bindService()时,若service还没有启动,则系统会先启动service运行在的新进程。
当然,如果没用aidl,bindservice可以去bind一个本进程内的service,此时,这个service运行在与调用bindService()
的客户端所在的同一个进程中,而通过serviceConnection返回的IBinder也是那个Stub,而不是BindProxy。
一般的调用方式为:
Intent intent = new Intent();mContext = this;intent.setClass(mContext, AccessoryOTAService.class);bindService(intent, conn, BIND_AUTO_CREATE);
在frameworks/base/core/java/android/bluetooth/BluetoothA2dp.java中有个较典型的调用示例:
synchronized (mConnection) { try { if (mService == null) { if (VDBG) Log.d(TAG,"Binding service..."); doBind(); } } catch (Exception re) { Log.e(TAG,"",re); }}
boolean doBind() { Intent intent = new Intent(IBluetoothA2dp.class.getName()); ComponentName comp = intent.resolveSystemService(mContext.getPackageManager(), 0); intent.setComponent(comp); if (comp == null || !mContext.bindServiceAsUser(intent, mConnection, 0, android.os.Process.myUserHandle())) { Log.e(TAG, "Could not bind to Bluetooth A2DP Service with " + intent); return false; } return true;}
synchronized (mConnection) { if (mService != null) { try { mService = null; mContext.unbindService(mConnection); } catch (Exception re) { Log.e(TAG,"",re); } }}
private final ServiceConnection mConnection = new ServiceConnection() { public void onServiceConnected(ComponentName className, IBinder service) { if (DBG) Log.d(TAG, "Proxy object connected"); mService = IBluetoothA2dp.Stub.asInterface(service); if (mServiceListener != null) { mServiceListener.onServiceConnected(BluetoothProfile.A2DP, BluetoothA2dp.this); } } public void onServiceDisconnected(ComponentName className) { if (DBG) Log.d(TAG, "Proxy object disconnected"); mService = null; if (mServiceListener != null) { mServiceListener.onServiceDisconnected(BluetoothProfile.A2DP); } }};
2. startService也可以启动一个跨进程的service,不同的是,startService()启动的service与调用方的生命绑定在一起,
即,如果调用方退出了,service也就退出了,bindService不是这样的,即使调用方退出了,service还可以存在。
0 0
- startService与bindService
- startService与bindService
- startService与bindService
- startService与bindService
- startService与bindService
- startService与bindService
- startService与bindService
- startService与bindService
- startService与bindService
- startService与bindService
- startService与bindService
- startService与bindService
- startService与bindService
- startService与bindService
- 理解 BindService 与 StartService
- startService与bindService
- startService与bindService
- bindService与startService
- poj2195
- 计算机网络高级软件编程技术
- ubuntu没有man pthread_mutex_init
- codeblock中没有gcc编译器的解决方案
- Light OJ 1193 Dice (II) (DP)
- bindService与startService
- iOS --- 根据字符串的实际大小来计算UILabel的size
- LVS的DR模式
- 多重背包转01背包/母函数(1171)
- 使用spring web flow遇到的问题-1
- UEFI模式下安装ubuntu14.04和windows8.1双系统
- iOS-网络七层模型&&网络数据包
- LeetCode Lowest Common Ancestor of a Binary Search Tree
- 为什么直接执行shell脚本可以,但crontab下执行错误