1. 引言:
(1)AIDL的作用
在Android平台,每个应用程序都是一个单独的JVM,都运行在自己的进程空间里, 通常,一个进程不允许访问另一个进程的内存空间(一个应用不能访问另一个应用)。当用户(程序开发人员)想在一个App中访问另一个App的进程空间的时候,就需要进程间通信。在Android中,远程服务为我们提供了实现进程间通信的方式,其中,AIDL是应用程序开发人员常的一种方式。
java代码如下:
QQService.java
之后再重新编译这个项目
(1)AIDL的作用
在Android平台,每个应用程序都是一个单独的JVM,都运行在自己的进程空间里, 通常,一个进程不允许访问另一个进程的内存空间(一个应用不能访问另一个应用)。当用户(程序开发人员)想在一个App中访问另一个App的进程空间的时候,就需要进程间通信。在Android中,远程服务为我们提供了实现进程间通信的方式,其中,AIDL是应用程序开发人员常的一种方式。
AIDL (Android Interface Definition Language) 是一种IDL 语言,用于生成可以在android设备上两个进程之间进行进程间通信(interprocess communication, IPC)的代码。如果在一个进程中(例如Activity)要调用另一个进程中(例如Service)对象的操作,就可以使用AIDL生成可序列化的参 数。换句比较浅显的话来说,就是我这个App应用的activity,需要调用其他App应用的Service.当然同一App应用的activity 与service也可以在不同进程间,这可以设置Service配置中,android:process=":remote"。
2.知识图谱
例子:实现QQ的登录功能
xml代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.cookie.android0715services2.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MyQQ"
android:textSize="20sp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/et_main_uname"
android:hint="请输入用户名"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/et_main_upass"
android:hint="请输入密码"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="login"
android:text="登录"/>
</LinearLayout>
java代码如下:
MainActivity.java
package com.example.cookie.android0715services2;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private EditText et_main_uname;
private EditText et_main_upass;
private Intent intent;
// private QQLoginService.MyIBinder myIBinder;
private QQLogin qqLogi;
private QQLoginInterface qqLoginInterface;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_main_uname = (EditText) findViewById(R.id.et_main_uname);
et_main_upass = (EditText) findViewById(R.id.et_main_upass);
intent = new Intent(this,QQLoginService.class);
}
ServiceConnection serviceConnection=new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.i("test","绑定成功");
// myIBinder = (QQLoginService.MyIBinder) service;
//qqLogi = (QQLogin) service;
qqLoginInterface = QQLoginInterface.Stub.asInterface(service);
}
@Override
public void onServiceDisconnected(ComponentName name) {
Log.i("test","绑定失败");
}
};
@Override
protected void onResume() {
super.onResume();
//绑定服务(Service.BIND_AUTO_CREATE绑定的时候自动创建)
bindService(intent,serviceConnection, Service.BIND_AUTO_CREATE);
}
public void login(View view) throws RemoteException {
String uname=et_main_uname.getText().toString();
String upass=et_main_upass.getText().toString();
boolean flag= qqLoginInterface.login(uname,upass);
if (flag){
Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(this, "登录失败", Toast.LENGTH_SHORT).show();
}
}
}
QQService.java
package com.example.cookie.android0715services2;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;
import android.widget.Toast;
/**
* Created by Administrator on 2017/7/15 0015.
*/
public class QQLoginService extends Service {
//把它想象成以前我们的dao层登录业务逻辑类
class MyIBinder extends QQLoginInterface.Stub{
public boolean login(String uname,String upass){
if ("10000".equals(uname) &&"123456".equals(upass)){
return true;
}else{
return false;
}
}
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
Log.i("test","onBind");
return new MyIBinder();
}
}
在java文件下new一个aidl文件
package com.example.cookie.android0715services2;
/**
* Created by Administrator on 2017/7/15 0015.
*/
public interface QQLogin {
public boolean login(String uname,String upass);
}
之后再重新编译这个项目
Build -------> Make Project
做一个微信的登录需要用到QQ的登录服务
xml代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.cookie.android0715wechat.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MyWeChat"
android:textSize="20sp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/et_main_uname"
android:hint="请输入用户名"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/et_main_upass"
android:hint="请输入密码"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="login"
android:text="登录"/>
</LinearLayout>
在java下新建一个文件夹,就是以你要启动的那个包名作为它的包名
跳转到project--->找到刚写的QQ登录项目----->generated----->source----->aidl---->debug
copy里面的文件复制到你在微信下新建的包下面
MainActivity.java代码如下
package com.example.cookie.android0715wechat;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import com.example.cookie.android0715services2.QQLoginInterface;
public class MainActivity extends AppCompatActivity {
private EditText et_main_uname;
private EditText et_main_upass;
private Intent intent;
private QQLoginInterface qqLoginInterface;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_main_uname = (EditText) findViewById(R.id.et_main_uname);
et_main_upass = (EditText) findViewById(R.id.et_main_upass);
intent = new Intent();
//componentName中的第一个双引号写的是你要启动的那个项目的包名,包名.服务类
ComponentName componentName=new ComponentName("com.example.cookie.android0715services2","com.example.cookie.android0715services2.QQLoginService");
intent.setComponent(componentName);
}
ServiceConnection serviceConnection=new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
//把你的自动生成的aidl的类copy到微信里面
qqLoginInterface = QQLoginInterface.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) {
String uname=et_main_uname.getText().toString();
String upass=et_main_upass.getText().toString();
boolean flag= false;
try {
flag = qqLoginInterface.login(uname,upass);
} catch (RemoteException e) {
e.printStackTrace();
}
if (flag){
Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(this, "登录失败", Toast.LENGTH_SHORT).show();
}
}
}