1、编写activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" > <Button
android:onClick="start"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="开启服务"
/> <Button
android:onClick="stop"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="停止服务"
/>
<Button
android:onClick="bind"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="绑定服务"
/> <Button
android:onClick="unbind"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="解除绑定服务"
/> <Button
android:onClick="change"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="调用方法,切换至下一首歌曲"
/> </LinearLayout>

2、编写MainActivity.java

package com.hyzhou.testservice;

import com.hyzhou.testservice.TestServer.MyBinder;

import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection; public class MainActivity extends Activity {
//步骤4:在activity里面得到服务IBinder对象的引用
private TestServer.MyBinder myBinder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
} public void start(View view)
{
Intent intent=new Intent(this,TestServer.class);
startService(intent); }
public void stop(View view)
{
Intent intent=new Intent(this,TestServer.class);
stopService(intent);
} public void bind(View view)
{
Intent intent=new Intent(this,TestServer.class);
/**
* intent 激活服务意图
* conn 代理人中间人对象,用来跟服务建立联系不能为空
* BIND_AUTO_CREATE 在绑定服务的时候 如果服务不存在就自动创建
*/
//步骤1:采用绑定的方式开启服务
bindService(intent, new MyConn(), BIND_AUTO_CREATE); }
public void unbind(View view)
{
Intent intent=new Intent(this,TestServer.class); } public void change(View view)
{
/**
* 由于系统框架在创建服务的时候会创建与之对应的上下文
* 下面的代码是直接new对象
* TestService service=new TestService();
* service.changeSing("月亮之上");
*/
//步骤5:利用IBinder对象间接调用服务里面的方法
myBinder.callchangeSing("月亮至上"); } private class MyConn implements ServiceConnection{ //在服务被成功绑定的时候调用的方法
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
System.out.println("testserver代理人返回回来了");
//步骤3:服务返回的IBinder对象会被传递给MyConn的回调方法
myBinder=(MyBinder)service; } //在服务失去绑定的时候调用的方法 只有程序异常终止才会调用该方法
@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub }
}
}

  3、编写TestServer服务

package com.hyzhou.testservice;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.widget.Toast; /**
*
*/ /**
* @author hyzhou
*
* 2013-12-10
*/
public class TestServer extends Service { @Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub System.out.println("服务被成功的绑定了------...");
//步骤2:服务在成功绑定的时候会调用onBind方法,返回一个IBinder对象
return new MyBinder();
}
public class MyBinder extends Binder{
@SuppressWarnings("unused")
public void callchangeSing(String name)
{
changeSing(name);
}
} @Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
System.out.println("开启服务------...");
} @Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
System.out.println("销毁服务------...");
} public void changeSing(String name)
{
Toast.makeText(getApplicationContext(), "切换至当前的音乐"+name, 0).show();
} }

  

绑定方式开始服务&调用服务的方法的更多相关文章

  1. Android(java)学习笔记229:服务(service)之绑定服务调用服务里面的方法 (采用接口隐藏代码内部实现)

    1.接口 接口可以隐藏代码内部的细节,只暴露程序员想暴露的方法 2.利用上面的思想优化之前的案例:服务(service)之绑定服务调用服务里面的方法,如下: (1)这里MainActivity.jav ...

  2. Android(java)学习笔记228:服务(service)之绑定服务调用服务里面的方法

    1.绑定服务调用服务里面的方法,图解: 步骤: (1)在Activity代码里面绑定 bindService(),以bind的方式开启服务 :                     bindServ ...

  3. Android(java)学习笔记172:服务(service)之绑定服务调用服务里面的方法 (采用接口隐藏代码内部实现)

    1. 接口 接口可以隐藏代码内部的细节,只暴露程序员想暴露的方法 2. 利用上面的思想优化之前的案例:服务(service)之绑定服务调用服务里面的方法,如下: (1)这里MainActivity.j ...

  4. Android(java)学习笔记171:服务(service)之绑定服务调用服务里面的方法

    1.绑定服务调用服务里面的方法,图解: 步骤: (1)在Activity代码里面绑定 bindService(),以bind的方式开启服务 :                     bindServ ...

  5. [android] 绑定方式开启服务&调用服务的方法

    需求:后台开启一个唱歌服务,这个服务里面有个方法切换歌曲 新建一个SingService继承系统Service 重写onCreate()和onDestory()方法 填一个自定义的方法changeSi ...

  6. Android--绑定服务调用服务的方法

    Service依照其启动的方式,可分为两种: 1.Started Started的Service.通过在Application里用startService(Intent intent)方法来启动.这样 ...

  7. SpringCloud实战-Feign声明式服务调用

    在前面的文章中可以发现当我们通过RestTemplate调用其它服务的API时,所需要的参数须在请求的URL中进行拼接,如果参数少的话或许我们还可以忍受,一旦有多个参数的话,这时拼接请求字符串就会效率 ...

  8. SpringCloud-Feign声明式服务调用

    在前面的文章中可以发现当我们通过RestTemplate调用其它服务的API时,所需要的参数须在请求的URL中进行拼接,如果参数少的话或许我们还可以忍受,一旦有多个参数的话,这时拼接请求字符串就会效率 ...

  9. Dubbo——服务调用过程

    文章目录 引言 服务的交互 服务降级 集群容错 服务调用 服务端接收请求 总结 引言 经过之前文章的铺垫,现在可以来分析服务的交互调用过程了. 服务的交互 服务降级 从名字上看我们不难理解MockCl ...

随机推荐

  1. Spring Cloud Config 配置中心 自动加解密功能 jasypt方式

    使用此种方式会存在一种问题:如果我配置了自动配置刷新,则刷新过后,加密过后的密文无法被解密.具体原因分析,看 SpringCloud 详解配置刷新的原理 使用  jasypt-spring-boot- ...

  2. <转> linux进程状态的说明

    我只是做一个mark,为了日后复习:http://blog.csdn.net/tianlesoftware/article/details/6457487 他写得非常的详细,值得推荐. 补充一点什么是 ...

  3. 关于Unity中如何立即中断动画然后重新开始播放

    今天做一个FPS游戏的时候,用的是新版的动画系统,遇到一个问题. 就是用枪打敌人的时候,敌人会播放一个被击中的动画,但是如果在动画播放的过程中再射击敌人,敌人会先把第一个被击中的动画播放完,才再播放第 ...

  4. Android中获取系统上安装的APP信息

    Version:0.9 StartHTML:-1 EndHTML:-1 StartFragment:00000099 EndFragment:00003259 Android中获取系统上安装的APP信 ...

  5. Spring Boot 启用Gzip压缩

    有两点需要注意 1.需要在application.properties里启用压缩,并设置压缩支持的格式(默认支持text/html等,但不支持application/json) server.comp ...

  6. eclipse debug 执行到断点处并没有停下,断点无效问题

    转自:http://blog.csdn.net/cuidiwhere/article/details/14434439 现象: 利用eclipse debug时,执行到断点处并没有停下.正常情况下,在 ...

  7. 【阿里云】WindowsServer2012 搭建FTP站点 图文记录

    配置说明: 服务商:阿里云 系统: WindowsServer2012 一:配置FTP服务器 1.进入操作系统,直接从启动栏打开服务器管理器,选择添加功能和角色 2.选择服务器 3.勾选FTP服务器选 ...

  8. C# 获取Image图片格式

    private void button1_Click_1(object sender, EventArgs e) { string path = Application.StartupPath; us ...

  9. array_multisort—对多个数组或多维数组进行排序

    From: http://www.cnblogs.com/lwbqqyumidi/archive/2013/01/31/2887188.html PHP中array_multisort可以用来一次对多 ...

  10. UITableView:可展开的 UITableView

    针对 TableView,有些时候需要在点击 cell 时,展开这行 cell,显现出更多的选项或者全部内容等. 比较容易想到的处理方案就是利用 section,在未选择之前,每一行都是一个 sect ...