Android Service之进程间通信(远程服务)

Android Service是分为两种:
  本地服务(Local Service): 同一个apk内被调用
  远程服务(Remote Service):被另一个apk调用
远程服务需要借助AIDL来完成。

AIDL 是什么
  AIDL (Android Interface Definition Language) 是一种IDL 语言,用于生成可以在Android设备上两个进程之间进行进程间通信(interprocess communication, IPC)的代码。如果在一个进程中(例如Activity)要调用另一个进程中(例如Service)对象的操作,就可以使用AIDL生成可序列化的参数。
  AIDL IPC机制是面向接口的,像COM或Corba一样,但是更加轻量级。它是使用代理类在客户端和实现端传递数据。

AIDL 的作用
  由于每个应用程序都运行在自己的进程空间,并且可以从应用程序UI运行另一个服务进程,而且经常会在不同的进程间传递对象。在Android平台,一个进程通常不能访问另一个进程的内存空间,所以要想对话,需要将对象分解成操作系统可以理解的基本单元,并且有序的通过进程边界。
  通过代码来实现这个数据传输过程是冗长乏味的,Android提供了AIDL工具来处理这项工作。

1.第一个apk的xml布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <EditText
        android:id="@+id/et_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <EditText
        android:id="@+id/et_pwd"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="登陆"
        android:onClick="login"/>
</LinearLayout>

2.MainActivity的java的代码
public class MainActivity extends AppCompatActivity {

private EditText et_name;
private EditText et_pwd;
private Intent intent;
private LoginInterfaceOut logininterfaceout;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    et_name = (EditText) findViewById(R.id.et_name);
    et_pwd = (EditText) findViewById(R.id.et_pwd);
    //服务路径
    intent = new Intent(this, Login.class);
}

ServiceConnection connection = new ServiceConnection() {
    //服务链接成功
    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        logininterfaceout = LoginInterfaceOut.Stub.asInterface(service);
    }

    //服务链接失败
    @Override
    public void onServiceDisconnected(ComponentName name) {

    }
};

@Override
protected void onResume() {
    super.onResume();
    //当activity重新开始时绑定服务
    bindService(intent, connection, Service.BIND_AUTO_CREATE);
}

public void login(View view) throws Exception {
    String name = et_name.getText().toString();
    String pwd = et_pwd.getText().toString();
    boolean flag=logininterfaceout.login(name,pwd);
    //吐司一下
    Toast.makeText(this, ""+flag, Toast.LENGTH_LONG).show();

}

}
3.注册个服务
public class Login extends Service {

//继承LoginInterfaceOut.Stub类
class MyIBinder extends LoginInterfaceOut.Stub{
@Override
public boolean login(String name, String pwd) throws RemoteException {
if (name.equals(“111”) && pwd.equals(“123”)) {
return true;
}
return false;
}
}

@Nullable
@Override
public IBinder onBind(Intent intent) {
    return new MyIBinder();
}

}
在AndroidManifest.xml清单文件中配置服务,放在application下。

<service android:name=".Login"
            android:exported="true">
        </service>

4.建一个AIDL文件
interface LoginInterfaceOut {
 boolean login(String name, String pwd);
}

5.第二apk的xml布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <EditText
        android:id="@+id/et_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <EditText
        android:id="@+id/et_pwd"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="登陆"
        android:onClick="login"/>
</LinearLayout>

2.MainActivity的java的代码
public class MainActivity extends AppCompatActivity {

    private EditText et_pwd;
    private EditText et_name;
    private Intent intent;
    private LoginInterfaceOut loginInterfaceOut;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et_name = (EditText) findViewById(R.id.et_name);
        et_pwd = (EditText) findViewById(R.id.et_pwd);
        intent = new Intent();
        //显式Intent:通过指定Intent组件名称来实现的,它一般用在知道目标组件名称的前提下,一般是在相同的应用程序内部实现的。
        //隐式Intent:通过Intent Filter来实现的,它一般用在没有明确指出目标组件名称的前提下,一般是用于在不同应用程序之间。
        //Android 5.0 之后,启动其他应用程序的服务,不允许使用隐式,但可以通过ComponentName来启动
        //第一个参数是第一个apk的包名,第二个参数是第一个apk的包名加你要调用那个服务的文件名
        ComponentName componentName = new ComponentName("com.zking.g150831_android23", "com.zking.g150831_android23.Login");
        intent.setComponent(componentName);
    }

    ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            loginInterfaceOut = LoginInterfaceOut.Stub.asInterface(service);
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };

    @Override
    protected void onResume() {
        super.onResume();
        bindService(intent, serviceConnection, Service.BIND_AUTO_CREATE);
    }

    public void login(View view) throws Exception {
        String name = et_name.getText().toString();
        String pwd = et_pwd.getText().toString();
        boolean flag = loginInterfaceOut.login(name, pwd);
        Toast.makeText(this, "" + flag, Toast.LENGTH_SHORT).show();
    }
}

6.在左上角把视图模式从Android切换成project模式,在第一个项目下面找到aidl文件把它复制到第二个项目java包里面,在二个项目下面建个一样的文件夹来装它。
这里写图片描述

这里写图片描述

这里写图片描述

这样就可以了,这两个项目都是登陆的过程,但是第二个项目掉的是第一个项目的服务来登陆的,服务判断全在第一个项目里。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值