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

时间:2023-11-23 21:55:08

1. 接口

接口可以隐藏代码内部的细节,只暴露程序员想暴露的方法

2. 利用上面的思想优化之前的案例:服务(service)之绑定服务调用服务里面的方法,如下:

(1)这里MainActivity.java:

 package com.itheima.bind;

 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 {
IService 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 = (IService) 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(600);
}
}

(2)其中的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();
}
/**
* 服务内部的小蜜,可以调用服务的方法
*
*/
private class MyBinder extends Binder implements IService{
/**
* 调用服务的方法。
* @param money 钱
*/
public void callMethodInService(int money){
if(money>500){
methodInService();
}else{
Toast.makeText(DemoService.this, "这点钱还想办事呀?", 0).show();
}
}
public void 打麻将(){
Toast.makeText(DemoService.this, "一起打麻将", 0).show();
}
public void 洗桑拿(){
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();
}
}

(3)接口IService.java:

package com.itheima.bind;

/**
* 定义的接口,暴露一个给钱办事的方法。
*/
public interface IService {
public void callMethodInService(int money);
}

其他文件代码不变

(4)工程一览图:

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