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

时间:2023-03-09 14:46:01
Android(java)学习笔记228:服务(service)之绑定服务调用服务里面的方法

1.绑定服务调用服务里面的方法,图解:

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

步骤:

(1)在Activity代码里面绑定 bindService(),以bind的方式开启服务 ;

                    bindServiceintent, new MyConn(), BIND_AUTO_CREATE);

参数intent:意图对象,服务对应的意图对象  new  Intent(this,Service.class)

参数ServiceConnection (接口,自定义其接口实现内部类MyConn() ):通讯频道,利用他可以获取服务成功绑定后得到的秘书

参数BIND_AUTO_CREATE:常量,服务不存在会自动创建

(2)实现MyConn接口实现内部类;

/**
* 服务连接成功的通讯频道
*
*/
private class MyConn implements ServiceConnection{
//当服务被成功连接的时候调用的方法
@Override
public void
onServiceConnected(ComponentName name, IBinder service) {
(重要参数IBinder,代表的就是中间人,服务的秘书)
}
//当服务失去连接的时候调用的方法
@Override
public void onServiceDisconnected(ComponentName name) { }
}

(3)如果服务被成功绑定  会执行onBind的方法       

public  IBinder onBind (Intent  intent )

这个方法的返回值为 IBinder 就是服务内部的秘书

(4)扩展实现服务内部的秘书,可以间接的调用服务的方法

      /**
* 服务内部的秘书,可以调用服务的方法
*
*/
public class MyBinder extends Binder{
/**
* 调用服务的方法。
* @param money 钱
*/
public void callMethodInService(int money){
if(money>500){
methodInService();
}else{
Toast.makeText(DemoService.this, "这点钱还想办事呀?", 0).show();
}
}
}

(5)在MyConn成功绑定的时候,就得到了IBInder对象, MyBinder

(6)利用MyBinder间接调用服务的方法

2.案例代码:

(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:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="bind"
android:text="绑定服务" /> <Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="unbind"
android:text="解除绑定服务" /> <Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="call"
android:text="调用服务里面的方法" /> </LinearLayout>

布局效果如下:

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

(2)MainActivity.java:

 package com.itheima.bind;

 import com.itheima.bind.DemoService.MyBinder;

 import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View; public class MainActivity extends Activity {
MyBinder myBinder; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
} protected void onDestory(Bundle savedInstanceState) {
super.onDestory();
----;//解绑服务
} /**
* 绑定服务,获取服务里面的秘书,间接的调用服务里面的方法。
* @param view
*/
public void bind(View view){
Intent intent = new Intent(this,DemoService.class);
//intent 意图
//conn 服务的通讯频道
//1 服务如果在绑定的时候不存在,会自动创建
System.out.println("1.采用bind的方式开启服务");
bindService(intent, new MyConn(), BIND_AUTO_CREATE);
} /**
* 解绑服务
* @param view
*/
public void unbind(View view){ System.out.println("解绑服务");
if(myBinder != null) {
unbindService(new MyConn());
myBinder = null;
} }
/**
* 服务连接成功的通讯频道
*
*/
private class MyConn implements ServiceConnection{
//当服务被成功连接的时候调用的方法
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
System.out.println("3. 得到了服务的一个连接,通讯频道,获取到服务内部的秘书");
myBinder = (MyBinder) service;
System.out.println("4.把ibinder强制类型转化成秘书MyBinder");
}
//当服务失去连接的时候调用的方法
@Override
public void onServiceDisconnected(ComponentName name) { }
} /**
* 调用服务里面的方法。
* @param view
*/
public void call(View view){
System.out.println("5.利用mybinder间接的调用服务的方法");
myBinder.callMethodInService(3000);
}
}

其中创建的服务DemoService.java如下:

 package com.itheima.bind;

 import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.widget.Toast; public class DemoService extends Service { /**
* 在服务被绑定的时候调用的方法
*
* IBinder 服务内部的秘书
*/
@Override
public IBinder onBind(Intent intent) {
System.out.println("2. 服务如果成功绑定会执行onbind,返回服务内部的秘书 mybinder");
return new MyBinder();
}
/**
* 服务内部的秘书,可以调用服务的方法
*
*/
public class MyBinder extends Binder{
/**
* 调用服务的方法。
* @param money 钱
*/
public void callMethodInService(int money){
if(money>500){
methodInService();
}else{
Toast.makeText(DemoService.this, "这点钱还想办事呀?", 0).show();
}
}
} /**
* 服务里面的方法
*/
public void methodInService(){
Toast.makeText(this, "哈哈,我是服务的方法,被你调用了。", 0).show();
} @Override
public void onCreate() {
System.out.println("服务被创建了");
super.onCreate();
}
@Override
public void onDestroy() {
System.out.println("服务被销毁了。");
super.onDestroy();
}
}

这里定义一个Service,当然要在AndroidMainfest.xml文件中注册一下:

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.itheima.bind"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" /> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.itheima.bind.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name="com.itheima.bind.DemoService"></service>
</application> </manifest>

3. 绑定服务的应用场景

 提供一个服务,后台运行,里面有一些公共的逻辑供调用.

>1. 微信支付, 微信有一个支付的服务,绑定,调用支付的方法
>2. sony手机,人脸识别的服务,绑定到这个服务传递一个照片就会把人脸标记出来
>3. 音乐播放器,后台服务里面播放音乐绑定服务暂停下一曲上一曲