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. 微信中关闭网页输入内容时的安全提示 [干掉 “防盗号或诈骗,请不要输入QQ密码”]

    未设置之前: 需要把域名加入白名单 设置方法:微信公共平台后台-->公众号设置--->功能设置--->填写业务域名即可.

  2. GRUB——系统的引导程序简单介绍

    这几天对于操作系统是如何引导启动的特征的感兴趣,已经到了不能自拔的状态了,所以索性好好了解一下: 前面已经说过了,MBR对于系统启动的重要性,这是不多啰嗦:  现在介绍一个 grub ,启动管理器,它 ...

  3. python写的读取json配置文件

    配置文件默认为conf.json 使用函数set完成追回配置项. 使用load或取配置项. 代码如下: #!/usr/bin/env python3 # -*- coding: utf-8 -*- ' ...

  4. 导入Maven项目后,Eclipse提示“Missing artifact ”类的错误

    导入Maven项目后,Eclipse提示“Missing artifact ”类的错误 标签: Maven Missing art 2016-08-15 16:05 679人阅读 评论(0) 收藏 举 ...

  5. 测试webservice的时候,如果出现这个错误:"The test form is only available for requests from the local machine"

    测试webservice的时候,如果出现这个错误:"The test form is only available for requests from the local machine&q ...

  6. erlang的erl文件的编码方式

    在数据源头的文件第一行加上%%coding: latin-1

  7. Ironic 安装和配置详解

    转自:http://amar266.blogspot.com/2014/12/ironic-installation-and-configuration.html 1.Install Openstac ...

  8. 前端图片压缩(纯js)

    html代码: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <t ...

  9. SELECT a.loginname,a.deviceid,a.time,Row_Number() OVER (partition by a.loginname ORDER BY a.deviceid desc,a.time asc) rank

    现在做一个反欺诈内容要用到笛卡尔积,用来分析用户一个手机号,对应的多个设备,每个更换设备的时间,这里取的时间是系统收集时间,用来代表更换的时间, 所以要先对设备换的时间作排序,然后进行rank,最后求 ...

  10. 你可能不知道UED和UCD

    我们都知道UI是User Interface,即它的本意是用户界面,从字面上看是用户和界面组成,实际上还包括用户与界面之间的交互关系.UI最初对大家来说只是一个名词,它代表一些界面.当然重点还是是UI ...