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. 彻底清除Github上某个文件以及历史

    注意:如下操作会删除选中的文件以及历史记录,若你想保留最新版本的记录,请做好备份. cd进入到你的本地项目文件夹,然后依次执行下面6行命令即可: git filter-branch --force - ...

  2. Mongo的备份和恢复(mongodump 和mongorestore )

    http://www.runoob.com/mongodb/mongodb-mongodump-mongorestore.html --备份单个表mongodump -u superuser -p 1 ...

  3. 微信小程序 --- e.currentTarget.dataset.id 获取不到值

    直接代码 wxml代码片段 <view class='ranksList' wx:for="{{ranksLb}}"> <view class='ranksLis ...

  4. Java设计模式(18)策略模式(Strategy模式)

    Strategy是属于设计模式中 对象行为型模式,主要是定义一系列的算法,把这些算法一个个封装成单独的类. Stratrgy应用比较广泛,比如,公司经营业务变化图,可能有两种实现方式,一个是线条曲线, ...

  5. Bootstrap小体验

    感觉很不错,给人一种清淅 明了的感觉

  6. 关于Unity中的刚体和碰撞器的相关用法(二)

    在关于Unity中的刚体和碰撞器的相关用法(一)的基础上 有一个plane平面,一个ball球体,都挂了碰撞器,ball挂了刚体Rigidbody,写了一个脚本ball挂载在球体上,球体从空中落下装机 ...

  7. Self20171218_Eclipse+TestNg HelloWorld

    作为一个经典的入门例子,这里展示如何开始使用TestNG单元测试框架. 使用的工具 : TestNG 6.8.7 Maven 3 Eclipse IDE TestNG下载并安装 从这里 http:// ...

  8. e836. 设置JTabbedPane中卡片的提示语

    There are two ways to set a tool tip on a tab. The first is to specify it when the tab is created; t ...

  9. e610. Setting Focus Traversal Keys in a Component

    When the focus is on a component, any focus traversal keys set for that component override the defau ...

  10. win10上跑 sqlserver 2000应用程序

    将SQL Server 安装程序\X86\SYSTEM\SQLUNIRL.DLL 替换到Win10 的 C:\windows\system32\目录下,64位win10 还要复制到SYSWOW64目录 ...