android服务
2016-07-29 17:17
337 查看
Service有两种启动模式:startService和bindService。
1、startService启动的服务,如果在Activity里面没有停止这个服务的话,Activity关闭之后Service还在。
2、bindService启动的服务,Activity关闭了之后Service也关闭了。 bind启动,unbind解除的话服务直接会Destroy。
3、startService和bindService组合使用,先startService启动服务,再bindService绑定服务,Activity与Service进行通信,解除绑定unbindService(此时服务没有destroy),停止服务stopService。
这样就可以实现通信了(同一进程)。
Service的的配置记得加在Application框里面
没有绑定服务就解除绑定,会报错。
绑定之后获得了服务的binder,就可以使用自定义的Binder里面的方法了。
1、startService启动的服务,如果在Activity里面没有停止这个服务的话,Activity关闭之后Service还在。
2、bindService启动的服务,Activity关闭了之后Service也关闭了。 bind启动,unbind解除的话服务直接会Destroy。
3、startService和bindService组合使用,先startService启动服务,再bindService绑定服务,Activity与Service进行通信,解除绑定unbindService(此时服务没有destroy),停止服务stopService。
这样就可以实现通信了(同一进程)。
Service的的配置记得加在Application框里面
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.binbin.testbinder"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name=".MyService"> <intent-filter> <action android:name="android.intent.action.MyService" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </service> </application> </manifest>
没有绑定服务就解除绑定,会报错。
绑定之后获得了服务的binder,就可以使用自定义的Binder里面的方法了。
相关文章推荐
- Android Studio你不知道的调试技巧
- Service的两种启动方式onstartService和onbindService区别
- 安卓计算下载速度
- Android内存优化之OOM
- Android拍照上传至PHP服务器并写入MySql数据库(下)
- Android EditText 限制输入数字和字母设置
- Android拍照上传至PHP服务器并写入MySql数据库(上)
- Android基础:res目录
- Android好用且常用的插件及工具
- Android Studio 常用的快捷键
- Android类似QQ左划出现删除按钮点击效果的ListView
- Android 防止OOM的一种方法
- 翻译:no more findViewById
- AndroidJNI 通过C++调用JAVA
- Android触摸事件总结
- 面向对象六大原则----里氏替换原则,依赖倒置原则
- Android Studio打包及引用aar
- 安卓中使用ThreadPoolExcutor
- Android之TextView控件的学习
- 【Android】炫酷的MaterialDesign Ripple 水波纹触摸动画