package com.example.myact10;

 import com.example.myact10.MyService.MyBinder;

 import android.support.v7.app.ActionBarActivity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.widget.Button;
/**
* Android bindService实现Activity和service的绑定
* @Describe:
* @package: com.example.myact10
* @author shaobn
* @date 2015-9-15 下午2:25:52
*/
public class MainActivity extends ActionBarActivity {
private Button myButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myButton = (Button) this.findViewById(R.id.button1);
myButton.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
ServiceConnection serviceConnection = new ServiceConnection() { @Override
public void onServiceDisconnected(ComponentName arg0) {
// TODO Auto-generated method stub
} @Override
public void onServiceConnected(ComponentName arg0, IBinder inBinder) {
// TODO Auto-generated method stub
MyBinder myBinder = (MyBinder)inBinder;
Log.i("String",myBinder.getData());
}
};
Intent intent = new Intent();
intent.setClass(MainActivity.this,MyService.class);
bindService(intent, serviceConnection,BIND_AUTO_CREATE);
}
});
} }
 package com.example.myact10;

 import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder; public class MyService extends Service {
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
MyBinder myBinder = new MyBinder();
return myBinder;
}
public class MyBinder extends Binder{
public String getData(){ return "onBind Service";
} } }

Android bindservice使用的更多相关文章

  1. android bindService()

    bindService简介 Service一般用于不用显示,运行在后台的服务. startService 是我们最常用的启动Service的方法.而如何让service与其他组件通信呢?一般在一个进程 ...

  2. Android BindService中遇到的一个小问题

    今天在使用BindService的时候遇到个小问题,我希望通过Bindservice获取到这个服务,然后执行服务内的某个自定义方法,如下: if(bindService==null){ Intent ...

  3. Android service ( 一 ) 三种开启服务方法

    一. Service简介 Service是android 系统中的四大组件之一(Activity.Service.BroadcastReceiver.ContentProvider),它跟 Activ ...

  4. 新建android系统服务

    一.Android系统服务 Android提供了很多系统服务:如ActivityManger,PowerManger,WindowManger,WifiManger等等. 这些服务都是系统启动开始就一 ...

  5. 深入理解Android的startservice和bindservice

    一.首先,让我们确认下什么是service?         service就是android系统中的服务,它有这么几个特点:它无法与用户直接进行交互.它必须由用户或者其他程序显式的启动.它的优先级比 ...

  6. 【Android 界面效果34】Android里Service的bindService()和startService()混合使用深入分析

    .先讲讲怎么使用bindService()绑定服务 应用组件(客户端)可以调用bindService()绑定到一个service.Android系统之后调用service的onBind()方法,它返回 ...

  7. Android开发之bindService()侦听service内部状态

    在Android开发之bindService()通信的基础上,实现bindService()方法侦听service内部状态. 实现侦听service内部状态,使用的是回调机制 1.首先实现一个接口 p ...

  8. 理解Android的startservice和bindservice(转)

    一.首先,让我们确认下什么是service? service就是android系统中的服务,它有这么几个特点:它无法与用户直接进行交互.它必须由用户或者其他程序显式的启动.它的优先级比较高,它比处于前 ...

  9. 【转】Android中BindService方式使用的理解

    原文网址:http://www.cnblogs.com/onlylittlegod/archive/2011/05/15/2046652.html 最近学习了一下Android里面的Service的应 ...

随机推荐

  1. Qt high DPI

    http://doc.qt.io/qt-5/highdpi.html Qt Support Ability to provide pixmaps or artwork for high resolut ...

  2. windows下安装和配置mongoDB

    上次在mac下安装和配置了mongodb,这次在windows下也尝试安装和配置mongodb. 1.首先下载mongodb压缩包,下载后解压到D盘或E盘.如下: 2.配置环境变量:桌面—计算机右键— ...

  3. [LeetCode]题解(python):060-Permutation Sequence

    题目来源 https://leetcode.com/problems/permutation-sequence/ The set [1,2,3,…,n] contains a total of n! ...

  4. Red and Black---hdu1312(dfs)

    2015-04-07http://acm.hdu.edu.cn/showproblem.php?pid=1312 Sample Input 6 9....#......#............... ...

  5. iOS面试题01

    1.#import和#include.@class有什么区别?#import<>和#import“”又有什么区别? 答:1.#import和#include都能完整地包含某个文件的内容,# ...

  6. LeetCode Find Peak Element

    原题链接在这里:https://leetcode.com/problems/find-peak-element/ 题目: A peak element is an element that is gr ...

  7. saltstack之(二)软件包下载安装

    由于salt组件的安装依赖较多,最好使用yum源安装(不建议使用源码安装).由于试验环境限制,不能使用网络,故增加了实验的难度.下面分可以访问internet和不可以访问internet两方面介绍sa ...

  8. 8、JavaScript深入浅出——数据类型

    一.六种数据类型 Javascript是弱类型. 五种原始类型和一种对象类型: number String boolean null undefined Object 二.隐式转换 +与-的运算举例: ...

  9. Maven 介绍

    1.maven的用途maven是一个项目构建和管理的工具,提供了帮助管理 构建.文档.报告.依赖.scms.发布.分发的方法.可以方便的编译代码.进行依赖管理.管理二进制库等等.maven的好处在于可 ...

  10. 第五篇 Replication:事务复制-How it works

    本篇文章是SQL Server Replication系列的第五篇,详细内容请参考原文. 这一系列包含SQL Server事务复制和合并复制的详细内容,从理解基本术语和设置复制的方法,到描述它是如何工 ...