一般情况下我们使用startService(Intent service)来启动一个服务,但这种情况下无法得到Service对象的引用,通过bindService方法启动服务则可以实现此功能。下面给一个小例子演示一下:
1.调用者
- package com.zhf.local;
-
-
- import android.app.Activity;
- import android.content.ComponentName;
- import android.content.Context;
- import android.content.Intent;
- import android.content.ServiceConnection;
- import android.os.Bundle;
- import android.os.IBinder;
-
-
-
-
-
-
- public class LocalServiceActivity extends Activity {
-
- private MyService myService;
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
-
- Intent intent = new Intent(this, MyService.class);
- bindService(intent, connection, Context.BIND_AUTO_CREATE);
- }
-
- private ServiceConnection connection = new ServiceConnection() {
-
- @Override
- public void onServiceDisconnected(ComponentName name) {
- myService = null;
- }
-
- @Override
- public void onServiceConnected(ComponentName name, IBinder service) {
- myService = ((MyService.MyBinder) service).getService();
- System.out.println("Service连接成功");
-
- myService.excute();
- }
- };
-
- protected void onDestroy() {
- super.onDestroy();
- unbindService(connection);
- };
- }
2.服务者
- package com.zhf.local;
-
- import android.app.Service;
- import android.content.Intent;
- import android.os.Binder;
- import android.os.IBinder;
-
- public class MyService extends Service {
- private final IBinder binder=new MyBinder();
- @Override
- public IBinder onBind(Intent intent) {
- return binder;
- }
-
- public class MyBinder extends Binder{
- MyService getService(){
- return MyService.this;
- }
- }
-
- public void excute(){
- System.out.println("通过Binder得到Service的引用来调用Service内部的方法");
- }
-
- @Override
- public void onDestroy() {
-
- super.onDestroy();
- }
-
- @Override
- public boolean onUnbind(Intent intent) {
-
- System.out.println("调用者退出了");
- return super.onUnbind(intent);
- }
- }
分享到:
相关推荐
主要介绍了Android bindservice失败解决方法的相关资料,希望通过本文能帮助大家解决这样的问题,需要的朋友可以参考下
后台服务,当系统推出,关闭后台服务 有问题来这里问我
多次调用bindService()方法并不会导致多次创建服务及绑定(也就是说onCreate()和onBind()方法并不会被多次调用,但是多次执行bindService)
Android中有两种主要方式使用Service,通过调用Context的startService方法或调用Context的bindService方法,本文只探讨纯bindService的使用,不涉及任何startService方法调用的情况。如果想了解startService相关的...
代码实现了绑定服务的三种方式,并用messenger 、aidl实现了进程间通信
目录里AIDLClient为service的使用方,MyApplication为service的提供方;服务很简单,仅提供加法计算;
Android四大组件之绑定Service详解,示例代码:附带博客地址: https://blog.csdn.net/lijianbiao0/article/details/85067100
Android Service与BroadcastReceiver、bindService示例详解,代码非常详细,详解博客网址:http://blog.csdn.net/sunboy_2050/article/details/7364024
NULL 博文链接:https://trylovecatch.iteye.com/blog/1163300
实例详解Android中startService()和bindService()的区别。 说明详见http://blog.csdn.net/a343902152/article/details/50857316
AIDL:Android Interface Definition Language,即Android接口定义语言。 Service跨进程传递数据需要借助aidl,主要步骤是这样的: 编写aidl文件,AS自动生成的java类实现IPC通信的代理 继承自己的aidl类,实现里面...
学习顺序 testService testBindService testIntenService testAidlClient+testAIDL
通过Binder调用server里的方法
Android中bindService调用服务的核心代码,有问题欢迎提出,共同进步。
android数据传输的Service BindService的一个demo,Service添加一个计数器,Acticity可以获取当前状态
(Android)Service的使用,Service与Activity的绑定