Android Service 通信
来源:互联网 发布:医院网络总监招聘 编辑:程序博客网 时间:2024/06/05 09:27
(转载)http://www.jianshu.com/p/b441d41272af
参考资料:Android 中 Service 通信
GitHub演示项目:Android Service 通信
1. 创建一个 Service
1.1 创建 Service 类
新建一个类,这里取名 MyService,继承自 Service
public class MyService extends Service { // 构造方法 public MyService() { } // 绑定服务时,执行该方法 @Override public IBinder onBind(Intent intent) { // TODO: Return the communication channel to the service. throw new UnsupportedOperationException("Not yet implemented"); } // 当服务开启时,调用的方法 @Override public void onCreate() { super.onCreate(); } // 当服务销毁时,调用的方法 @Override public void onDestroy() { super.onDestroy(); } /** * 当每次使用 startService 调用服务时,调用该方法(但一个服务同时只会存在一个实例) * 通过 Intent 传递的数据,也会在这里得到 * 注意和 onCreate() 方法的区别: * onCreate() 一个应用开启服务时,该方法只会调用一次 * onStartCommand() 只要使用 startService() 调用此服务,该方法就会执行一次 */ @Override public int onStartCommand(Intent intent, int flags, int startId) { return super.onStartCommand(intent, flags, startId); }}
1.2 在 AndroidManifest.xml 中注册该服务
<service android:name=".MyService" android:enabled="true" android:exported="true" ></service>
在 AndroidManifest.xml 里 Service 元素的常见选项:
选项名 作用 android:name服务类名android:label服务的名字,如果此项不设置,那么默认显示的服务名则为类名android:icon服务的图标android:permission申明此服务的权限,这意味着只有提供了该权限的应用才能控制或连接此服务android:process表示该服务是否运行在另外一个进程,如果设置了此项,那么将会在包名后面加上这段字符串表示另一进程的名字android:enabled如果此项设置为 true,那么 Service 将会默认被系统启动,不设置默认此项为 falseandroid:exported表示该服务是否能够被其他应用程序所控制或连接,不设置默认此项为 false2. 开启|关闭 Service
2.1 通过 startService() | stopService 方法开启和关闭服务
开启服务代码如下:
// 新建一个 IntentIntent intent = new Intent(this, MyService.class);// 这里可以给 Intent 里面放置数据,数据对应在 Service 类中 onStartCommand 方法参数 Intent 可以得到// 开启服务startService(intent);
关闭服务代码如下:
// 新建一个 IntentIntent intent = new Intent(this, MyService.class);// 关闭服务stopService(intent);
2.2 通过 bindService() | unbindService() 方法开启和关闭服务
开启服务代码如下:
// 新建一个 IntentIntent intent = new Intent(this, MyService.class);// 开启服务bindService(i, this, BIND_AUTO_CREATE);
此时,Service 要修改onBind() 方法,返回一个实现 IBinder 接口的对象:
@Overridepublic IBinder onBind(Intent intent) { // Android 官方提供的 Binder 对象,不过为了实现 Service 和 Activity 互相通信,之后我们要自己重新实现一个类 return new Binder();}
并且,Activity 中需要实现 ServiceConnection 接口中 onServiceConnected() 和 onServiceDisconnected() 两个方法,这两个方法也是 Service 和 Activity 相互通信的关键,代码如下:
// 服务连接时,调用该方法,其中 IBinder 对象,就是上面 onBind() 方法中返回的对象@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {}// 服务断开连接时,调用该方法@Overridepublic int onStartCommand(Intent intent, int flags, int startId) { return super.onStartCommand(intent, flags, startId);}
关闭服务代码如下:
unbindService(this);
3. Activity 和 Service 通信
3.1 Activity -> Service 通信
3.1.1 通过 Intent 通信
该方法是在开启服务时通过将数据放置到 Intent 中,传递给 Service,代码如下:
Intent intent = new Intent(this, MyService.class);// 为服务传递数据intent.putExtra("input", "data");startService(intent);
Service 则可以从 onStartCommand(Intent intent, int flags, int startId) 参数 intent 可以得到相信数据, 代码如下:
@Overridepublic int onStartCommand(Intent intent, int flags, int startId) { String data = intent.getStringExtra("input"); return super.onStartCommand(intent, flags, startId);}
3.1.2 通过 Binder 通信
首先在 Service 类中新建一个内部类 Binder 并继承自 Android.os.Binder,在其中实现获得数据的方法,代码如下:
public class Binder extends android.os.Binder { public void setData(String d) { // data 为 Service 类的属性,在内部类中可以很方便的访问到外部的属性 data = d; }}
接着,在 Activity 中添加一个 Binder 属性,并通过 onServiceConnected(ComponentName name, IBinder service) 方法的参数对其进行赋值,代码如下:
private MyService.Binder binder = null;@Overridepublic void onServiceConnected(ComponentName name, IBinder service) { // 这里要强制类型转换一下 binder = (MyService.Binder) service;}
最后,便可以在想对 Service 属性修改的地方调用之前设置的 set 方法,代码如下:
if (binder != null) { binder.setData("123");}
系统会在 Service 启动时,对 binder 进行赋值,成功启动后,便可以顺利地实现数据传递
3.2 Service -> Activity 通信
要想实现 Service 对 Activity 的通信,需要用到回调函数
首先在 Service 中新建一个接口,代码如下:public static interface CallBack { void getServiceData(String data);}
其次在 Service 中新建一个 CallBack 属性,并设置 set、get 方法,代码如下:
private CallBack callBack = null;public void setCallBack(CallBack callBack) { this.callBack = callBack;}public CallBack getCallBack() { return callBack;}
然后在需要传值的地方调用接口方法,代码如下:
// 需要传值的地方if (callBack != null) { // 将需要传递的值作为参数 callBack.getServiceData(data);}
接着在 Activity 中为 Service 设置 CallBack 并实现 getServiceData() 方法,那么怎么获得 Service 对象呢?
我们仍然可以通过 Binder 来获得 Service 对象,在 Binder 类中添加如下方法:public MyService getService() { return MyService.this;}
在 Activity 中的 onServiceConnected() 方法中获得 Service 对象,并设置 CallBack,代码如下:
@Overridepublic void onServiceConnected(ComponentName name, IBinder service) { binder = (MyService.Binder) service; binder.getService().setCallBack(new MyService.CallBack() { @Override public void getServiceData(String data) { // 这里的 data 参数便是从 Service 传递过来的数据 } });}
Android笔记
作者:Coder_Y
链接:http://www.jianshu.com/p/b441d41272af
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
- Android service通信
- Android 远程Service通信
- Android Service 通信
- Android Service通信详解
- Android Service通信
- Android Service通信
- android Service通信
- Android Service 通信
- android service 与activity通信--绑定service
- Android中的Service的通信
- android Service 跨进程通信
- android activity与service通信
- Android Activity与Service通信
- android Service 跨进程通信
- Android service与Activity通信
- android activity 与service通信
- Android service 与 activity通信
- Android Service与Activity通信
- 一个好用的 poi封装类
- 电容式触摸屏调试常见问题汇总(连载贴)
- mtk android mm编译单个app模块
- 自顶向下,逐步求精
- 计算素数小于非负数的数目,n。
- Android Service 通信
- 大众对于“大数据”的相关伦理观点
- CSS3实现渐变色字体
- Spark将大量分区写入HDFS报错
- lintcode&九章算法——100. 删除有序数组里的重复元素 ? 待解决
- Entity framework查询原理
- webrtc持续更新
- andriod—junit测试框架
- git 遇到The remote end hung up unexpectedly