aidl是android內(nèi)部進(jìn)程通信接口的描述語言,通過它我們可以定義進(jìn)程間的通信接口,一種本地代碼技術(shù)
創(chuàng)建AIDL服務(wù)與使用AIDL服務(wù)舉例,
一、AIDL服務(wù)的項(xiàng)目圖
實(shí)現(xiàn)的步驟為:
1.創(chuàng)建IMyService.aidl文件,內(nèi)容為
package com.fs.aidl.service;
interface IMyService
{
String getValue(String s);
}
該文件放入到com.fs.aidl.service文件夾后,會自動在gen下com.fs.aidl.service中生成IMyService.java文件
2.創(chuàng)建MyService.java文件,內(nèi)容為:
package com.fs.aidl.service;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
public class MyService extends Service {
public class MyServiceImpl extends IMyService.Stub {
// @Override
public String getValue(String s) throws RemoteException {
//寫出自已的業(yè)務(wù)邏輯代碼
return "華清遠(yuǎn)見: "+s;
}
}
@Override
public IBinder onBind(Intent intent) {
return new MyServiceImpl();
}
}
3、AndroidManifest.xml內(nèi)容為
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="//schemas.android.com/apk/res/android"
package="com.fs.aidl.service"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".MyService" >
<intent-filter>
<action android:name="com.fs.aidl.service.MyService" />
</intent-filter>
</service>
</application>
<uses-sdk android:minSdkVersion="4" />
</manifest>
二、使用AIDL服務(wù)的項(xiàng)目圖
1、android5_4aidlService項(xiàng)目中的MyService.java連同包一起復(fù)制出來粘貼在android5_4aidlClient項(xiàng)目中
2、main.xml內(nèi)容為
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="//schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="@+id/textview"
android:textColor="#FFFF0000"
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:textSize="36dp" />
<Button android:id="@+id/btnBindAIDLService"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:text="綁定AIDL服務(wù)" android:layout_marginTop="10dp" />
<Button android:id="@+id/btnInvokeAIDLService"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:text="調(diào)用AIDL服務(wù)" android:layout_marginTop="10dp" />
</LinearLayout>
3、MainActivity的內(nèi)容:
package com.fs.aidl.client;
import com.fs.aidl.service.IMyService;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class UserActivity extends Activity implements OnClickListener {
private IMyService myService = null;
private Button btnInvokeAIDLService;
private Button btnBindAIDLService;
private TextView textView;
private ServiceConnection serviceConnection = new ServiceConnection() {
// @Override
public void onServiceConnected(ComponentName name, IBinder service) {
myService = IMyService.Stub.asInterface(service);
btnInvokeAIDLService.setEnabled(true);
}
// @Override
public void onServiceDisconnected(ComponentName name) {
}
};
// @Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btnBindAIDLService:
this.bindService(
new Intent("com.fs.aidl.service.MyService"),
serviceConnection, Context.BIND_AUTO_CREATE);
break;
case R.id.btnInvokeAIDLService:
try {
textView.setText(myService.getValue("xx"));
} catch (Exception e) {
}
break;
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnInvokeAIDLService = (Button) findViewById(R.id.btnInvokeAIDLService);
btnBindAIDLService = (Button) findViewById(R.id.btnBindAIDLService);
btnInvokeAIDLService.setEnabled(false);
textView = (TextView) findViewById(R.id.textview);
btnInvokeAIDLService.setOnClickListener(this);
btnBindAIDLService.setOnClickListener(this);
}
}
三、測試過程
1、安裝android5_4aidlService項(xiàng)目到手機(jī)中
2、安裝并運(yùn)行android5_4aidlClient項(xiàng)目
3、定擊“綁定AIDL服務(wù)”按扭,得到圖
4、點(diǎn)擊“調(diào)用AIDL服務(wù)”按鈕,得到圖
四、完
熱點(diǎn)新聞
課程問答