平时通过activity启动服务后,activity对于服务就没有任何控制权了,也就是无法调用服务里面的方法
通过绑定服务的方式,就把启动服务的activity和service绑定在一起,这样就可以在activity调用服务的方法
实现中间人的接口,不要接口也是可以,不过有了接口就方便远程调用,中间人类似于代理。
package com.itheima.servicelife;
/**
* 中间人的接口定义
*
*/
public interface IMiddlePerson {
/**
* 代办暂住证
* @param money
*/
public void callMethodInService(int money);
}
1.第一步服务要暴露方法 必须要有一个中间人
2.实现服务成功绑定的代码 ,返回一个中间人。
3.activity采用绑定的方式去开启服务。
4. 当服务被连接的时候调用 服务别成功 绑定的时候调用
5.通过中间人调用服务里面的方法。
package com.itheima.servicelife;
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 {
private MyConn conn ;
private IMiddlePerson mp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//绑定服务
public void bind(View view){
//3.activity采用绑定的方式去开启服务。
Intent intent = new Intent(this,MyService.class);
conn = new MyConn();
bindService(intent, conn, BIND_AUTO_CREATE);
}
//解除绑定服务
public void unbind(View view){
unbindService(conn);
}
@Override
protected void onDestroy() {
System.out.println("啊啊啊,我是activity,我挂了");
super.onDestroy();
}
//调用服务里面的方法。
public void call(View view){
//5.通过中间人调用服务里面的方法。
mp.callMethodInService(55);
}
private class MyConn implements ServiceConnection{
//4. 当服务被连接的时候调用 服务别成功 绑定的时候调用
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
System.out.println("在activity里面成功得到了中间人");
mp = (IMiddlePerson) service;
}
//当服务失去连接的时候调用(一般进程挂了,服务被异常杀死)
@Override
public void onServiceDisconnected(ComponentName name) {
}
}
}
package com.itheima.servicelife;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.widget.Toast;
public class MyService extends Service {
//2.实现服务成功绑定的代码 ,返回一个中间人。
@Override
public IBinder onBind(Intent arg0) {
System.out.println("服务被成功绑定了。。。。");
return new MiddlePerson();
}
@Override
public boolean onUnbind(Intent intent) {
System.out.println("onunbind");
return super.onUnbind(intent);
}
@Override
public void onCreate() {
System.out.println("oncreate");
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
System.out.println("onstartcommand");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
System.out.println("ondestory");
super.onDestroy();
}
/**
* 这是服务里面的一个方法
*/
public void methodInService(){
Toast.makeText(this, "哈哈,服务给你办好了暂住证。", 0).show();
}
//1.第一步服务要暴露方法 必须要有一个中间人
private class MiddlePerson extends Binder implements IMiddlePerson{
/**
* 代办暂住证
* @param money 给钱 50块钱以上才给办。
*/
public void callMethodInService(int money){
if(money>=50){
methodInService();
}else{
Toast.makeText(getApplicationContext(), "多准备点钱。", 0).show();
}
}
/**
* 陪领导打麻将
*/
public void playMajiang(){
System.out.println("陪领导打麻将。");
}
}
}
<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="fill_parent"
android:layout_height="wrap_content"
android:onClick="bind"
android:text="绑定服务" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="unbind"
android:text="解除绑定服务" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="call"
android:text="调用服务里面的方法" />
</LinearLayout>
绑定远程服务。。
绑定服务是虽然让activity能调用服务里面的方法,但是也有弊端,就是activity销毁了会直接影响到服务
所以同时设置开启服务和绑定服务就能实现长期运行又能调用服务的想法,按需进行切换即可
service开发课程一样,不过service和activity不在一个应用里面,所以是远程服务。
activity通过aidl文件远程获取中间人来完成操作。
除了获取中间人方式不一样,其他开发过程基本和绑定服务开发操作一样
package com.itheima.bindremote;
import com.itheima.remoteservice.IMiddlePerson;
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.os.RemoteException;
import android.view.View;
public class MainActivity extends Activity {
private MyConn conn;
private IMiddlePerson iMp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/**
* 绑定远程服务
* @param view
*/
public void bind(View view){
Intent intent = new Intent();
//参数是aidl文件所在的包名
intent.setAction("com.itheima.remoteservice");
conn = new MyConn();
bindService(intent, conn, BIND_AUTO_CREATE);
}
private class MyConn implements ServiceConnection{
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
iMp = IMiddlePerson.Stub.asInterface(service);
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
}
public void call(View view){
try {
iMp.callMethodInService();
} catch (RemoteException e) {
//远程服务的异常
e.printStackTrace();
}
}
@Override
protected void onDestroy() {
unbindService(conn);
super.onDestroy();
}
}
绑定远程服务实例:
绑定远程服务的一端,通过aidl获取远程服务的中间代理对象
<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="fill_parent"
android:layout_height="wrap_content"
android:onClick="click"
android:text="买5个炮弹" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="start"
android:text="开启服务" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="stop"
android:text="停止服务" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="bind"
android:text="绑定服务" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="unbind"
android:text="解除绑定服务" />
</LinearLayout>
开启服务和绑定服务的区别
package com.itheima.fish;
import com.itheima.alipay.ISafePay;
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.os.RemoteException;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity {
private ISafePay iSafePay;
private MyConn conn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Intent intent = new Intent();
// intent.setAction("com.itheima.alipay");
// startService(intent);
//保证服务长期后台运行。
}
//开启服务是保证服务后台长期运行
public void start(View view){
Intent intent = new Intent();
intent.setAction("com.itheima.alipay");
startService(intent);
}
public void stop(View view){
Intent intent = new Intent();
intent.setAction("com.itheima.alipay");
stopService(intent);
}
//绑定服务是调用服务里面的方法
public void bind(View view){
Intent intent = new Intent();
intent.setAction("com.itheima.alipay");
conn = new MyConn();
bindService(intent, conn, BIND_AUTO_CREATE);//异步的操作
}
public void unbind(View view){
unbindService(conn);
}
public void click(View view){
Intent intent = new Intent();
intent.setAction("com.itheima.alipay");
conn = new MyConn();
bindService(intent, conn, BIND_AUTO_CREATE);//异步的操作
//绑定服务调用服务的方法。
}
private class MyConn implements ServiceConnection{
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
iSafePay = ISafePay.Stub.asInterface(service);
//因为bindService是异步的,不知道什么时候会给iSafePay赋值,为了防止iSafePay为空
//所以就干脆把支付操作放在这里来执行
try {
boolean result = iSafePay.callPay(System.currentTimeMillis(), "123", 3.52f);
if(result){
Toast.makeText(getApplicationContext(), "支付成功,获取大炮弹", 0).show();
}else{
Toast.makeText(getApplicationContext(), "支付失败,请重试", 0).show();
}
//支付成功了服务就没必要存在了,为了节约内存就干脆解除服务
// unbindService(conn);
// conn = null;
} catch (RemoteException e) {
e.printStackTrace();
}
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
}
}
被绑定服务的一端:
提供aidl来提供中间代理对象
MainActivity类是没有任何用处的。
package com.itheima.alipay;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.os.RemoteException;
public class SafePayService extends Service {
@Override
public IBinder onBind(Intent intent) {
System.out.println("服务被绑定 onbind");
return new MyBinder();
}
/**
* 安全支付的方法
*/
private boolean pay(long time,String pwd,double money){
if("123".equals(pwd)){
return true;
}else{
return false;
}
}
private class MyBinder extends ISafePay.Stub{
/**
* 调用安全支付的逻辑
*/
@Override
public boolean callPay(long time, String pwd, double money)
throws RemoteException {
return pay(time, pwd, money);
}
}
@Override
public void onCreate() {
System.out.println("oncreate支付宝服务被创建,一直在后台运行,检查手机的安全状态");
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
System.out.println("服务onstart");
return super.onStartCommand(intent, flags, startId);
}
@Override
public boolean onUnbind(Intent intent) {
System.out.println("onunbind");
return super.onUnbind(intent);
}
@Override
public void onDestroy() {
System.out.println("ondestory支付宝服务被销毁");
super.onDestroy();
}
}