AILD机制:
通俗的讲就是访问别人的服务。
使用情况:
访问别人的第三方服务框架
例如:
自己的Activity访问别人的service
案例:跨应用进程绑定访问service
服务端
1、编写服务端
android_day11_service
PlayMusicService.java
package com.example.android_day11_service;
import com.example.android_day11_service.MusicInterface.Stub;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
public class PlayMusicService extends Service{
/**
* 当调用bindService时执行
*/
public IBinder onBind(Intent intent) {
return new MyBinder();
}
//等第二步编写完接口之后
class MyBinder extends Stub{
public void play() throws RemoteException {
Log.i("info", "play...");
}
public void next() throws RemoteException {
Log.i("info", "next...");
}
public void pre() throws RemoteException {
Log.i("info", "pre...");
}
}
}
2、编写接口也就是AIDL(MusicInterface.aidl)
MusicInterface.aidl
后缀名改了之后根文件自动生成文件
package com.example.android_day11_service;
/**
* 当前接口 就是 aidl接口
* 在该接口中需要定义供客户端访问的抽象方法
*/
interface MusicInterface {
void play();
void next();
void pre();
}
点击进入根文件自动生成的文件:
3、在清单文件中注册
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android_day11_service"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<service android:name="com.example.android_day11_service.PlayMusicService">
<intent-filter >
<action android:name="com.tarena.action.MUSIC_SERVICE"/>
</intent-filter>
</service>
</application>
</manifest>
4、运行安装在手机上
客户端
android_day11_phone
1、新建Activity(PlayMusicActivity.java)
package com.example.android_day11_phone;
import com.example.android_day11_service.MusicInterface;
import com.example.android_day11_service.MusicInterface.Stub;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.app.Activity;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.view.Menu;
import android.view.View;
public class PlayMusicActivity extends Activity {
private ServiceConnection conn;
private MusicInterface musicInterface;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play_music);
// 执行service绑定
Intent intent = new Intent("com.tarena.action.MUSIC_SERVICE");
conn = new ServiceConnection() {
public void onServiceDisconnected(ComponentName name) {
}
// 绑定成功时执行
public void onServiceConnected(ComponentName name, IBinder service) {
musicInterface = Stub.asInterface(service);
}
};
this.bindService(intent, conn, Service.BIND_AUTO_CREATE);
}
@Override
protected void onDestroy() {
super.onDestroy();
this.unbindService(conn);
}
public void doClick(View view) {
try {
switch (view.getId()) {
case R.id.button1:
musicInterface.play();
break;
case R.id.button2:
musicInterface.next();
break;
case R.id.button3:
musicInterface.pre();
break;
}
} catch (RemoteException e) {
e.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.play_music, menu);
return true;
}
}
2、客户端想要用,要将服务端AIDL文件复制到客户端(包名一致)
3、运行