import android.os.Bundle;
import android.os.IBinder;
import android.app.Activity;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class ServiceBindActivity extends Activity {
  Button bind;
  Button unbind;
  Button getServiceStatus;
  //保持所启动的Service的IBinder对象
  BindService.MyBinder binder;
  //定义一个ServiceConnection对象
  private ServiceConnection conn = new ServiceConnection() {

    //当该Activity与Service断开连接时回调该方法
    @Override
    public void onServiceDisconnected(ComponentName name) {
      System.out.println("--Service Disconnected--");

    }

    //当该Activity与Service连接成功时回调该方法
    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
      System.out.println("--Service Coonnected");
      //获取Service的onBind方法所返回的MyBinder
      binder = (BindService.MyBinder)service;
    }
  };

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_service_bind);
    //获取程序界面中的start、stop、getServiceStatus按钮
    bind = (Button) findViewById(R.id.bind);
    unbind = (Button) findViewById(R.id.unbind);
    getServiceStatus = (Button) findViewById(R.id.getServiceStatus);
    //创建启动Service的Intent
    final Intent intent = new Intent();
    //为Intent设置Action属性
    intent.setAction("mediaprovider.service.BIND_SERVICE");
    bind.setOnClickListener(new OnClickListener() {

      @Override
      public void onClick(View v) {
      // 绑定指定Service
      bindService(intent, conn, Service.BIND_AUTO_CREATE);
    }
  });
  unbind.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
      //解除绑定Service
      unbindService(conn);
    }
  });
  getServiceStatus.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
      // 获取并显示Service的count
      Toast.makeText(ServiceBindActivity.this,
        "Service的count值为:"+binder.getCount(), 4000).show();
    }
  });
}

}

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

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

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

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

    绑定Service需要调用 public boolean bindService (Intent service, ServiceConnection conn, int flags): 传入一个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. Android手机tcpdump抓包

    在开发过程中遇到问题时,无法非常方便的获取到数据包,导致分析解决问题比较麻烦.这里介绍如何在Android手机上实现tcpdump抓包.   1.root机器  在用tcpdump抓包过程中,需要使用 ...

  2. golang json

    1.Go语言的JSON 库 Go语言自带的JSON转换库为 encoding/json 1.1)其中把对象转换为JSON的方法(函数)为 json.Marshal(),其函数原型如下 func Mar ...

  3. double精度问题,数据范围

    浮点数在计算机中存储方式  http://www.cnblogs.com/jillzhang/archive/2007/06/24/793901.html 1. double:  1bit(符号位) ...

  4. python网络编程socket之多线程

    #coding:utf-8 __author__ = 'similarface' import os,socket,threading,SocketServer SERVER_HOST='localh ...

  5. 程序猿必知会的JavaScript 的遍历方式

    不管是移动移动端开发还是web端开发,我们对JS的使用频率都在增加,今天小编将要和大家分享的就是JavaScript中,遍历方式的一些实现方法,个人感觉还是很有用的,有兴趣的童鞋可以一起来看看. 为了 ...

  6. Working with Data » Getting started with ASP.NET Core and Entity Framework Core using Visual Studio » 排序、筛选、分页以及分组

    Sorting, filtering, paging, and grouping 7 of 8 people found this helpful By Tom Dykstra The Contoso ...

  7. js到处excel

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  8. ajax 动态获取json的例子

    1.前台脚本: //用于切换图片列表的ajax function changePhoto(title,hotelId){ $.ajax({ contentType: "application ...

  9. noip赛前小结2

    嗯...赛前的第二份小结. 总结一下应该做的几个事情就好了. (1)关于做题顺序 做题顺序是很重要的. 开始的时候先审题,看清数据范围什么的,随便想一想,大概估计一下自己的得分. 第二题再把每道题仔细 ...

  10. 打通B/S与C/S !让HTML5 WebSocket与.NET Socket公用同一个服务端!

    随着HTML5 WebSocket技术的日益成熟与普及,我们可以借助WebSocket来更加方便地打通BS与CS -- 因为B/S中的WebSocket可以直接连接到C/S的服务端,并进行双向通信.如 ...