serverapp
MainActivity
package bw.com.serverapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
MyService
package bw.com.serverapp;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.annotation.Nullable;
/**
* 自定义的服务
*/
public class MyService extends Service{
MyAidl.Stub stub = new MyAidl.Stub() {
@Override
public String getInfo() throws RemoteException {
return "鹅鹅鹅, 曲项向天歌,白毛浮绿水,红掌拨清波!!!";
}
};
@Nullable
@Override
public IBinder onBind(Intent intent) {
return stub;
}
}
布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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:layout_width="match_parent"
android:layout_height="match_parent" tools:context="bw.com.serverapp.MainActivity">
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="Hello World!" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
MyAidl.aidl
// MyAidl.aidl
package bw.com.serverapp;
// Declare any non-default types here with import statements
//TODO 接口的声明和方法的申明都没有修饰符
interface MyAidl {
String getInfo();
}
清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="bw.com.serverapp">
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher"
android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true" android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!--注册Service-->
<service android:name=".MyService">
<intent-filter>
<action android:name="com.bw.1511c.aidl"/>
</intent-filter>
</service>
</application>
</manifest>
App
MainActivity
package bw.com.bw_goover_01.demo05;
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.widget.TextView;
import bw.com.bw_goover_01.R;
import bw.com.serverapp.MyAidl;
/**
* 通过aidl 获取服务端中, 返回的数据, 显示在TextView中
*/
public class Main5Activity extends AppCompatActivity {
private TextView mTv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main5);
mTv = (TextView) findViewById(R.id.tv_id);
}
//TODO 检测服务绑定是否成功
private ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
//服务绑定成功的回调方法
try {
MyAidl myAidl = MyAidl.Stub.asInterface(service);
String str = myAidl.getInfo();//获取数据
mTv.setText(str);
} catch (RemoteException e) {
e.printStackTrace();
}
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
//TODO 绑定服务
@Override
protected void onStart() {
super.onStart();
Intent intent = new Intent();
intent.setAction("com.bw.1511c.aidl");
//5.0 以后, 指定包名
intent.setPackage("bw.com.serverapp");
bindService(intent,connection,BIND_AUTO_CREATE);
}
//TODO 解绑服务
@Override
protected void onStop() {
super.onStop();
unbindService(connection);
}
}
布局
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="bw.com.bw_goover_01.demo05.Main5Activity">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/tv_id"
android:text="显示获取到的内容"
android:textSize="30sp"
android:textColor="@color/colorAccent"
/>
</LinearLayout>
AIDL的简单使用
Android Interface Definition Language 安卓接口定义语音
作用: 实现了跨进程间的通信 --- 两个App , 绑定服务
1, 服务端 ServerApp
1.1 在src/main 目录中, 创建一个aidl 文件 .aidl结尾
1.2 在文件中, 定义一个方法, 没有修饰符
1.3 编译-- .aidl 编译成 同名的.java文件
1.4 声明一个服务类, 绑定服务的方法
1.5 在清单文件中注册 -- 提供action动作
2, 客户端
2.1 复制, 服务端的src/main 目录中的aidl文件夹, 复制到客户端的同级目录中
2.2 编译客户端中的aidl文件 , 生成.java文件
2.3 绑定服务