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. SQL Server批量数据导出导入BCP使用

    BCP简介 bcp是SQL Server中负责导入导出数据的一个命令行工具,它是基于DB-Library的,并且能以并行的方式高效地导入导出大批量的数据.bcp可以将数据库的表或视图直接导出,也能通过 ...

  2. Python 扫面文件夹中的文件

    #coding=utf-8 import os,sys reload(sys) sys.setdefaultencoding("utf-8") def scan_files_sub ...

  3. How To Set Up Apache with a Free Signed SSL Certificate on a VPS

    Prerequisites Before we get started, here are the web tools you need for this tutorial: Google Chrom ...

  4. git 初次使用

    其实知道git很久了,也一度看了不少资料来学习指令.但是一直不明白到底我该咋办,我最疑惑的地方在于,本地代码是如何存储到远程服务器上的,那些指令在什么环境下执行,其实主要是目录问题.就是我在git s ...

  5. sell - 配置service

    1. 2. 注意value!

  6. java时间戳转date(转)

    1.时间戳的定义 时间戳(timestamp),通常是一个数字序列,唯一地标识某一刻的时间,指格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起 ...

  7. SVN Working Copy locked ,并且进行clean up也还是不行

    标题:working copy locked 提示:your working copy appears to be locked. run cleanup to amend the situation ...

  8. LeetCode Peeking Iterator

    原题链接在这里:https://leetcode.com/problems/peeking-iterator/ 题目: Given an Iterator class interface with m ...

  9. java实现读取文件内容(不同类型)

    在一些项目中大量的数据经常需要从文件中读取,例如xml文件,txt文件,csv文件 1.读取本地的xml文件,需要注意对应的路径 //读取xml文件,xmlFile为读取文件的路径 DocumentB ...

  10. Java多线程 wait, notify 和 notifyAll

    Java的Object类 public class Object { public final native void notify(); public final native void notif ...