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. 使用多种客户端消费WCF RestFul服务(一)——服务端

    RestFul风格的WCF既然作为跨平台.跨语言.跨技术的一种方式出现,并且在ASP.NET API流行起来之前还是架构的首选技术之一,那么我们就来简要的介绍一下WCF在各个平台客户端的操作. 开发工 ...

  2. 创建podSpec,使用pod管理第三方库

    提要: podfile文件会先读取.podspec文件,根据.podspec文件的指向来下载第三方库到项目中. 本文先通过一.二.三项,这三个步骤讲解了如何建立一个.podspec文件在本地.coco ...

  3. [LeetCode] Scramble String(树的问题最易用递归)

    Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...

  4. Sql Server中通配符

    Sql Server中通配符的使用 通配符_ "_"号表示任意单个字符,该符号只能匹配一个字符."_"可以放在查询条件的任意位置,且只能代表一个字符.一个汉字只 ...

  5. 终于有人把P2P、P2C、O2O、B2C、B2B、C2C 的区别讲透了

    http://news.mbalib.com/story/88506 P2P.P2C .O2O .B2C.B2B. C2C,每天看着这些常见又陌生的名词,如果有人跟你说让你解释它的含义,金融的小伙伴们 ...

  6. rem适配

    //REM适配new function() { var _self = this; _self.width = 640; // 设置默认最大宽度 _self.fontSize = 100; // 默认 ...

  7. Basic motion detection and tracking with Python and OpenCV

    http://www.pyimagesearch.com/2015/05/25/basic-motion-detection-and-tracking-with-python-and-opencv/

  8. OSGMFC

    在OSG的Demo中找到MFC_OSG类文件. #pragma once #include <osgViewer/Viewer> #include <osgViewer/Viewer ...

  9. git merge

    1. git 解决冲突 ***** <<<<<<< HEAD *** *** ======= **** **** ** >>>>> ...

  10. loadview 方法调用

    从官方文档看 不应该直接调用此方法,当控制器当前的view为空的时候,这个方法就会被调用,并且会创建一个空白的view,如果viewcontroller绑定了storyboard或者xib就不用说了. ...