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. Linux用户态和内核态
  2. IIS上部署MVC网站,打开后ExtensionlessUrlHandler-Integrated-4.0解决方法IIS上部署MVC网站,打开后500错误
  3. C# 获得MP4时长
  4. linux系统date命令详解
  5. 2016国赛B题小区数据爬取软件
  6. java socket 一个服务器对应多个客户端,可以互相发送消息
  7. eclipse的svn插件安装方式
  8. linux 挂载 镜像文件
  9. BZOJ 1827: [Usaco2010 Mar]gather 奶牛大集会( dp + dfs )
  10. fread读入优化,寻找速度极限
  11. Android初学:Gradle &#39;HelloWorld&#39; project refresh failed
  12. Mac安装sqlite3
  13. HTTPS 原理浅析及其在 Android 中的使用
  14. Dynamics 365权限变化大部署后需要注意什么?
  15. C++的ch1&amp;ch2的整理
  16. [2018.05].NET Core 3 and Support for Windows Desktop Applications
  17. 最简单的社交分享代码,无需注册OpenID
  18. Spring boot 错误处理机制
  19. Linux系统服务之inetd
  20. 整理Lua和Unity和Lua交互文章链接

热门文章

  1. R语言进行数据预处理
  2. sublime text 2使用方法
  3. thinkphp 对百度编辑器里的内容进行保存
  4. Axure RP Pro 7.0 注册码
  5. JBMP学习引导
  6. Linux命令_用户身份切换
  7. [转]Android--多线程之Handler
  8. Maven依赖机制
  9. e804. 显示JProgressBar的完成百分率
  10. 成都传智播客java就业班(14.04.01班)就业快报(Java程序猿薪资一目了然)