绑定Service需要调用

public boolean bindService (Intent service, ServiceConnection conn, int flags);

传入一个ServiceConnection 对象,该对象是一个接口,实例化时需要实现该接口,它的作用就是获得Service的IBinder对象,通过IBinder对象可以实现与Service的通信。

Service的的代码:

 package com.example.servicetest;

 import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log; public class BindServiceTest extends Service {
private int count = 0;
private boolean quit = false;
private MyBinder mBinder = new MyBinder(); public class MyBinder extends Binder {
public int count() {
return count;
}
} @Override
public IBinder onBind(Intent arg0) {
Log.i("csx", "onBind");
return mBinder;
} @Override
public void onCreate() { super.onCreate();
Log.i("csx", "onCreate");
new Thread() { @Override
public void run() {
while (!quit) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) { e.printStackTrace();
}
count++; } } }.start();
} @Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("csx", "onStartCommand");
return START_STICKY;
} @Override
public boolean onUnbind(Intent intent) {
Log.i("csx", "onUnbind");
return true;
} @Override
public void onDestroy() {
super.onDestroy();
this.quit = true;
Log.i("csx", "onDestroy");
} }

组件的代码:

 package com.example.servicetest;

 import com.example.servicetest.BindServiceTest.MyBinder;

 import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast; public class MainActivity extends ActionBarActivity {
public static final String SERVICE_ACTION = "com.example.servicetest.BindServiceTest";
Button bindButton, unbindButton, stateButton;
ServiceConnection conn;
BindServiceTest.MyBinder mBinder;
boolean isBind = false; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bindButton = (Button) findViewById(R.id.button_bind);
unbindButton = (Button) findViewById(R.id.button_unbind);
stateButton = (Button) findViewById(R.id.button_get_state); conn = new ServiceConnection() { @Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.i("csx", "onServiceConnected");
mBinder = (MyBinder) service; // 连接service之后将service中实现的Binder返回给本地声明的Binder } @Override
public void onServiceDisconnected(ComponentName name) {
Log.i("csx", "onServiceDisconnected");// 意外断开连接会调用该函数 } }; bindButton.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
Intent service = new Intent();
service.setAction(SERVICE_ACTION);
bindService(service, conn, BIND_AUTO_CREATE);// 绑定服务
isBind = true; }
}); unbindButton.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
if (!isBind) {
Toast.makeText(MainActivity.this, "请先绑定服务", Toast.LENGTH_SHORT).show();
return; } unbindService(conn);// 解除绑定服务
isBind = false; }
}); stateButton.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
if (!isBind) {
Toast.makeText(MainActivity.this, "请先绑定服务", Toast.LENGTH_SHORT).show();
return; }
Toast.makeText(MainActivity.this, "" + mBinder.count(), Toast.LENGTH_SHORT).show();// 通过mBinder获取service内部的数据 }
}); }
}

绑定本地Service并与之通信的更多相关文章

  1. Android菜鸟的成长笔记(18)——绑定本地Service并与之通信

    在上一篇中介绍了Service与Activity的区别及Service两种启动方式中的第一种启动方式startService(). 我们会发现用startService().stopService() ...

  2. 绑定本地Service并与之通信-----之二

    import android.os.Bundle;import android.os.IBinder;import android.app.Activity;import android.app.Se ...

  3. 绑定本地Service并与之通信-----之一

    import android.app.Service;import android.content.Intent;import android.os.Binder;import android.os. ...

  4. Android开发学习之路-Service和Activity的通信

    在很多时候,Service都不仅仅需要在后台运行,还需要和Activity进行通信,或者接受Activity的指挥,如何来实现,来看代码. 定义一个服务 // 创建一个服务,然后在onBind()中返 ...

  5. Android Service、IntentService,Service和组件间通信

    Service组件 Service 和Activity 一样同为Android 的四大组件之一,并且他们都有各自的生命周期,要想掌握Service 的用法,那就要了解Service 的生命周期有哪些方 ...

  6. 绑定本地的Session

    绑定本地的Session图示解析: 代码的结构: 代码: SaveServlet.java package com.itheima.servlet; import java.io.IOExceptio ...

  7. C# 开源一个基于 yarp 的 API 网关 Demo,支持绑定 Kubernetes Service

    关于 Neting 刚开始的时候是打算使用微软官方的 Yarp 库,实现一个 API 网关,后面发现坑比较多,弄起来比较麻烦,就放弃了.目前写完了查看 Kubernetes Service 信息.创建 ...

  8. 【Service Fabric】小白入门记录 本地Service Fabric集群安装及设置

    本篇内容是自学自记,现在我还不知道Service Fabric究竟是怎么个入门法,反正按照入门教程先进行本地Service Fabric集群的安装,万里路始于足下,要学习总得先把环境装好了才能开始学习 ...

  9. socket的bind函数是不是只能绑定本地IP,不能绑定外网IP么?

    参考: https://bbs.csdn.net/topics/391024376 别瞎猜测. 所谓bind,就是指绑定本地接受端口. 指定ip,是为了分辨多ip主机. --------------- ...

随机推荐

  1. hdoj 1012 u Calculate e

    u Calculate e Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  2. Oracle Tnsping慢

    http://www.linuxidc.com/Linux/2014-02/96167.htm http://www.askmaclean.com/archives/dns%E8%AE%BE%E7%B ...

  3. "判断this指针是不是null有什么意义呢"

    博客搬到了fresky.github.io - Dawei XU,请各位看官挪步.最新的一篇是:"判断this指针是不是null有什么意义呢".

  4. Android_消息机制

    Android通过Looper.Handler来实现消息循环机制. Android的消息循环是针对线程的,每个线程都可以有自己的消息队列和消息循环. Android系统中的Looper负责管理线程的消 ...

  5. cocos2d-x 的CCObject与autorelease 之深入分析

    转自: http://blog.csdn.net/honghaier/article/details/8160519 CCObject.h: #ifndef __CCOBJECT_H__ #defin ...

  6. 解决div和父div不上对齐

    加一个vertical-align: top;就好了.原因就是inline-block会使元素向下对齐.这和padding-top,margin-top没有关系的.使用浮动就不会有这种情况了,当然会带 ...

  7. SVProgressHUD 用法

    SVProgressHUD 是一个第三方的控件,是一个弹出提示层,用来提示 网络加载 或 提示对错,看下面图,你就明白了:     那么,SVProgressHUD 都有什么特点呢:   1. 提示当 ...

  8. AWS s3 python sdk code examples

    Yet another easy-to-understand, easy-to-use aws s3 python sdk code examples. github地址:https://github ...

  9. 【42】了解typename的双重意义

    1.在template声明中,class与typename是等价的,但是使用typename更好. 2.在template实现中,模版形参是从属名称,嵌套在模版形参中的类型是嵌套从属名称,不依赖任何t ...

  10. Sql Server2005 Transact-SQL 窗口函数(OVER)

    1.简介: SQL Server 2005中的窗口函数帮助你迅速查看不同级别的聚合,通过它可以非常方便地累计总数.移动平均值.以及执行其它计算.窗口函数功能非常强大,使用起来也十分容易.可以使用这个技 ...